简体   繁体   中英

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

So my string looks like this:

Basic information, advanced information, super information, no information

I would like to capture everything up to second comma so I get:

Basic information, advanced information

What would be the regex for that?

I tried: (.*,.*), but I get

Basic information, advanced information, super information,

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.

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.

I would take a DRY approach, like this:

^([^,]*,){1}[^,]*

This way you can match everything until the n occurrence of a character without repeating yourself except for the last pattern.

Although in the case of the original poster, the group and repetition of the group is useless I think this will help others that need to match more than 2 times the pattern.

Explanation:

  • ^ From the start of the line
  • ([^,]*,) Create a group matching everything except the comma character until it meet a comma.
  • {1} Count the above pattern (the number of time you need)-1. So if you need 2 put 1, if you need 20 put 19.
  • [^,] * Repeat the pattern one last time without the tailing comma.

Try this approach:

(.*?,.*?),.*

Link to the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM