[英]Regex with a match between quotes
I tried to find a solution to my RegEx problem, but I cannot find an answer.我试图找到解决我的 RegEx 问题的方法,但我找不到答案。
My target string is like this:我的目标字符串是这样的:
"Elevator" <sip:103@192.168.1.1>;tag=343483547
I want my matching groups to be: - Elevator - 103我希望我的匹配组是: - 电梯 - 103
But when I use this regex:但是当我使用这个正则表达式时:
"(.*)"?<sip:(\d*)@.*
then I get an unwanted quote with my 1st match.然后我在第一场比赛中收到了不需要的报价。 It results in: - Elevator" - 103
结果是: - 电梯” - 103
I can't get it to work.我无法让它工作。 I tried the most obvious things like
我尝试了最明显的事情,比如
"([^"]*)"?<sip:(\d*)@.*
but then the first match vanishes completely但是第一场比赛完全消失了
What am I doing wrong.我究竟做错了什么。
You are making the "
optional and the .*
is greedy matching until the <
is encountered.您正在使
"
成为可选,并且.*
是贪婪匹配,直到遇到<
。
If the quote is unwanted, you could update it to make the " not optional and match the space before the <
.如果引号是不需要的,您可以更新它以使 " 不可选并匹配
<
之前的空格。
The "(.*?)"
part can also be matched as ([^"]+)
"(.*?)"
部分也可以匹配为([^"]+)
If you want to match 1 or more digits use \\d+
如果要匹配 1 个或多个数字,请使用
\\d+
"(.*?)" <sip:(\d+)@
Regex demo (Click on the Table tab) 正则表达式演示(单击表选项卡)
You need to account for the whitespace between an optional "
and the following <
char.您需要考虑可选的
"
和以下<
字符之间的空格。
Use用
"([^"]*)"?\s*<sip:(\d*)@.*
See the regex demo查看正则表达式演示
Details细节
"
- a "
char "
- 一个"
字符([^"]*)
- Group 1: any zero or more chars other than "
([^"]*)
- 第 1 组:除"
之外的任何零个或多个字符"?
- an optional "
char "?
- 一个可选的"
字符\\s*
- 0+ whitespaces \\s*
- 0+ 个空格<sip:
- <sip:
substring <sip:
- <sip:
子串(\\d*)
- Group 2: zero or more (replace *
with +
to match one or more) digits (\\d*)
- 第 2 组:零个或多个(用+
替换*
以匹配一个或多个)数字@.*
- a @
and the rest of the line. @.*
- @
和该行的其余部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.