Regex match outer nested tags

前端 未结 4 898
南笙
南笙 2021-01-16 03:58

I have this string

blabla [IC=\"test\"]Hello, [IC=\"testing\"] this is a nested tag [EC], cya.[EC] blabla

I\'m trying to match the outer [

相关标签:
4条回答
  • 2021-01-16 04:07

    Try this:

    (\[IC="\w*"\].*\[EC\])
    

    https://regex101.com/r/MVU9ni/1

    Matches 0 or more characters that are located in between [IC="any_word_character_here"] and [EC].

    0 讨论(0)
  • 2021-01-16 04:10

    I dont get your decision to use the \s\S and it just seems more confusing than it really should be. However to fix the issue you are having simply remove the ?

    ...([\s\S]*?)\[EC]...
    

    to

    ...([\s\S]*)\[EC]...
    

    resulting into

    \[IC=\"([\s\S]*?)\"]([\s\S]*)\[EC]\n{0,1}
    

    however i would simply use \w and .* because they are easier

    \[IC="(\w*)"\](.*)\[EC\]
    

    EDIT:

    Working off of the assumption that you actually need to get the string value from [IC=".."] and string inbetween. otherwise if you just need whole data then no need for the groupings

    \[IC="\w*"\].*\[EC\]
    
    0 讨论(0)
  • 2021-01-16 04:14

    The regular expression below should do the trick:

    [^\[]*(\[.*\])[^\]]*
    

    You can try a working demo visiting this link.

    Alternatively, if you don't want to rely on capturing groups, this should be enough:

    (\[.*\])
    

    A working demo of the regular expression above can be tested here.

    0 讨论(0)
  • 2021-01-16 04:27

    You could use a recursive pattern (supported in PCRE):

    \[IC[^][]*\]
    (?:(?:[^][]*|(?R))*)
    \[EC\]
    

    See a demo on regex101.com.

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