如何匹配所有字符直到第二次出现? [英] How to match everything up to the second occurrence of a character?

查看:60
本文介绍了如何匹配所有字符直到第二次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的字符串看起来像这样:

基本信息、高级信息、超级信息、无信息

我想捕获直到第二个逗号的所有内容,所以我得到:

基本信息、高级信息

那个的正则表达式是什么?

我试过:(.*,.*), 但我得到

基本信息、高级信息、超级信息,

解决方案

这将捕获最多但不包括第二个逗号:

[^,]*,[^,]*

英文翻译:

  • [^,]* = 尽可能多的非逗号字符

  • , = 逗号

  • [^,]* = 尽可能多的非逗号字符

[...] 是一个字符类.[abc] 表示a 或 b 或 c",[^abc] 表示除 a 或 b 或 c 之外的任何内容.

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.

这篇关于如何匹配所有字符直到第二次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆