How to match everything up to the second occurrence of a character?

前端 未结 2 591
深忆病人
深忆病人 2020-12-15 17:49

So my string looks like this:

Basic information, advanced information, super information, no information

I would like to capture everything

相关标签:
2条回答
  • 2020-12-15 18:14

    This will capture up to but not including the second comma:

    [^,]*,[^,]*
    

    English translation:

    • [^,]* = as many non-comma characters as possible

    • , = a comma

    • [^,]* = as many non-comma characters as possible

    [...] is a character class. [abc] means "a or b or c", and [^abc] means anything but a or b or c.

    0 讨论(0)
  • 2020-12-15 18:17

    You could try ^(.*?,.*?), The problem is that .* is greedy and matches maximum amount of characters. The ? behind * changes the behaviour to non-greedy.

    You could also put the parenthesis around each .*? segment to capture the strings separately if you want.

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