简体   繁体   English

如何将所有内容匹配到字符的第二次出现?

[英]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. [abc]表示“a 或 b 或 c”,而[^abc]表示除 a 或 b 或 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:我会采用 DRY 方法,如下所示:

^([^,]*,){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.尽管在原始海报的情况下,分组和分组的重复是没有用的,我认为这将有助于其他需要匹配超过 2 次模式的人。

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. {1}计算上述模式(您需要的次数)-1。 So if you need 2 put 1, if you need 20 put 19.所以如果你需要 2 放 1,如果你需要 20 放 19。
  • [^,] * Repeat the pattern one last time without the tailing comma. [^,] * 最后一次重复该模式,不带尾逗号。

Try this approach:试试这个方法:

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

Link to the solution链接到解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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