正则表达式匹配给定字符后的文本,不包括字符本身 [英] Regex to match text after a given character excluding the character itself

查看:48
本文介绍了正则表达式匹配给定字符后的文本,不包括字符本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下文本 -

[Event "F/S Return Match"]

我要提取-

F/S Return Match

现在我正在使用 -

\"(.*)"

哪个收益 -

"F/S Return Match"

然后我用 -

[^"]*

得到 -

F/S Return Match

我可以将两者合二为一吗?

Could I combine the two into one?

推荐答案

Look-around 可能是一个选项:

Look-around could be an option:

(?<=")[^"]*(?=")

(?<=") 检查前一个字符是否为 ".
(?=") 检查下一个字符是否为 ".

(?<=") checks that the previous character is a ".
(?=") checks that the next character is a ".

测试.

另一种方法是使用分组:

"([^"]*)"

如何提取组取决于使用的语言.

How to extract the group is dependent on the language used.

测试.(注意匹配组"区域)

Test. (note the "Matching groups" area)

我没有简单地使用 "(.*)" 因为字符串 abc "def" "ghi" 将匹配 "def" "ghi",尽管您可能希望分别匹配 "def""ghi".[^"] 的替代方法是非贪婪匹配 - "(.*?)",它将匹配尽可能少的字符串.

I didn't simply use "(.*)" because the string abc "def" "ghi" will match "def" "ghi", though you may have wanted to match "def" and "ghi" separately. An alternative to [^"] is non-greedy matching - "(.*?)", which will match as little of the string as possible.

这篇关于正则表达式匹配给定字符后的文本,不包括字符本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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