简体   繁体   English

如何在正则表达式中排除一行的一部分

[英]How to exclude parts of a line in regex

Out of the line Your name is: "Foo Bar" I want to select Foo Bar in regex only.行外Your name is: "Foo Bar"我只想在正则表达式中使用 select Foo Bar

I have tried non-capturing groups without success: (?:^Your name is: ").*(?:")$我尝试过非捕获组但没有成功: (?:^Your name is: ").*(?:")$

"(.*?)" Works but I don't want the double quotes to be selected "(.*?)"有效,但我不想选择双引号

As you tagged pcre , you don't have to use a lookbehind assertion but you can match the text and then use \K to forget what is matched so far.当您标记pcre时,您不必使用 lookbehind 断言,但您可以匹配文本,然后使用\K忘记到目前为止匹配的内容。

^Your name is: "\K.+(?="$)

See a regex demo .请参阅正则表达式演示

Or without lookarounds at all and a capture group:或者根本没有环顾四周和捕获组:

^Your name is: "(.+)"$

See another regex demo .请参阅另一个正则表达式演示

You can use lookbehind and lookahead :您可以使用lookbehindlookahead

(?<=^Your name is: ").+(?="$)

(?<= looks behind for Your name is: " and (?= looks ahead for " . (?<= looks behind for Your name is: " and (?= looks ahead for "

The result will be whatever is between that.结果将介于两者之间。

regex101正则表达式101

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

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