How to capture multiline regex between two tags?

alias51

What's the best way to select all text between 2 comment tags? E.g.

<!-- Text 1
     Text 2
     Text 3
-->

<\!--.* will capture <!-- Text 1 but not Text 2, Text 3, or -->

Edit As per Basti M's answer, <\!--((?:.*\n)*)--> will select everything between the first <!-- and last -->. I.e. lines 1 to 11 below.

How would I modify this to select just lines within separate tags? i.e. lines 1 to 4:

1 <!-- Text 1 //First
2      Text 2
3      Text 3
4 -->
5
6 More text
7 
8 <!-- Text 4
9      Text 5
10     Text 6
11 -->         //Last
KeyNone

Depending on your underlying engine use the s-modifier (and add --> at the end of your expression.
This will make the . match newline-characters aswell.

If the s-flag is not available to you, you may use

<!--((?:.*\r?\n?)*)-->

Explanation:

<!--         #start of comment
  (           #start of capturing group
    (?:       #start of non-capturing group
      .*\r?\n? #match every character including a line-break
    )*        #end of non-capturing group, repeated between zero and unlimited times
  )           #end of capturing group
-->           #end of comment

To match multiple comment blocks you can use

/(?:<!--((?:.*?\r?\n?)*)-->)+/g

Demo @ Regex101

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex to find words between two tags

From Dev

Regex to capture characters including whitespace between tags

From Dev

Powershell replace regex between two tags

From Dev

How to match all text between two strings multiline

From Dev

Removing all the characters between two specific tags (java regex)

From Dev

How to remove the space between two <span> tags?

From Dev

Multiline capture regex fails if ungreedy

From Dev

Regex to match multiline text between two words including the words

From Dev

How to capture a string between two tags

From Dev

Grep/Sed between two tags with multiline

From Dev

Using grgit to capture logs between two tags

From Dev

How to list commits directly between two tags

From Dev

Find strings between two tags with regex in Qt

From Dev

Capture number between two whitespaces (RegEx)

From Dev

How to remove text between two tags?

From Dev

Powershell replace regex between two tags

From Dev

regex find and replace between two tags in sublime

From Dev

Regex Capture between a delimiter

From Dev

Find strings between two tags with regex in Qt

From Dev

How to capture two regex

From Dev

Regex capture text between two dynamic texts

From Dev

Bash. How to get multiline text between tags

From Dev

How to use regex to replace text between tags

From Dev

Python regex - capture text between two words as string, then append to list

From Dev

Multiline RegEx read between two strings

From Dev

RegEx - How to select all text in between backes (multiline)

From Dev

How to extract text between two different xml tags multiline

From Dev

How to extract text between two headings with regex, requires complicated non-capture groups

From Dev

how to extract portion of a string between two substrings in a multiline string in python

Related Related

  1. 1

    Regex to find words between two tags

  2. 2

    Regex to capture characters including whitespace between tags

  3. 3

    Powershell replace regex between two tags

  4. 4

    How to match all text between two strings multiline

  5. 5

    Removing all the characters between two specific tags (java regex)

  6. 6

    How to remove the space between two <span> tags?

  7. 7

    Multiline capture regex fails if ungreedy

  8. 8

    Regex to match multiline text between two words including the words

  9. 9

    How to capture a string between two tags

  10. 10

    Grep/Sed between two tags with multiline

  11. 11

    Using grgit to capture logs between two tags

  12. 12

    How to list commits directly between two tags

  13. 13

    Find strings between two tags with regex in Qt

  14. 14

    Capture number between two whitespaces (RegEx)

  15. 15

    How to remove text between two tags?

  16. 16

    Powershell replace regex between two tags

  17. 17

    regex find and replace between two tags in sublime

  18. 18

    Regex Capture between a delimiter

  19. 19

    Find strings between two tags with regex in Qt

  20. 20

    How to capture two regex

  21. 21

    Regex capture text between two dynamic texts

  22. 22

    Bash. How to get multiline text between tags

  23. 23

    How to use regex to replace text between tags

  24. 24

    Python regex - capture text between two words as string, then append to list

  25. 25

    Multiline RegEx read between two strings

  26. 26

    RegEx - How to select all text in between backes (multiline)

  27. 27

    How to extract text between two different xml tags multiline

  28. 28

    How to extract text between two headings with regex, requires complicated non-capture groups

  29. 29

    how to extract portion of a string between two substrings in a multiline string in python

HotTag

Archive