Regex: To pull out a sub-string between two tags in a string

后端 未结 9 1935
情书的邮戳
情书的邮戳 2020-12-08 00:35

I have a file in the following format:

Data Data
Data
[Start]
Data I want
[End]
Data

I\'d like to grab the Data I want from between the

相关标签:
9条回答
  • 2020-12-08 00:56

    Well, if you guarantee that each start tag is followed by an end tag then the following would work.

    \[start\](.*?)\[end\]
    

    However, If you have complex text such as the follwoing:

    [start] sometext [start] sometext2 [end] sometext [end]
    

    then you would run into problems with regex.

    Now the following example will pull out all the hot links in a page:

    '/<a(.*?)a>/i'
    

    In the above case we can guarantee that there would not be any nested cases of:

    '<a></a>'
    

    So, this is a complex question and can't just be solved with a simple answer.

    0 讨论(0)
  • 2020-12-08 01:07

    Refer to this question to pull out text between tags with space characters and dots (.)

    [\S\s] is the one I used

    Regex to match any character including new lines

    0 讨论(0)
  • 2020-12-08 01:11
    \[start\]\s*(((?!\[start\]|\[end\]).)+)\s*\[end\]
    

    This should hopefully drop the [start] and [end] markers as well.

    0 讨论(0)
提交回复
热议问题