简体   繁体   English

正则表达式-匹配引号之间的字符串以及引号之前的字符串

[英]Regex - match string between quotes but also what precedes it

If I have a file that I need to search through for every occurrence of this: 如果我有一个文件需要搜索以查找每次出现的情况:

firstName="string"

I know how to match the string between the quotes, but I would like to know how to match the variations of what precedes it. 我知道如何匹配引号之间的字符串,但我想知道如何匹配其前面的变体。 Like: 喜欢:

firstName ="string"
firstName = "string"

So, basically, I need to get all the strings, but I encountered a problem when there are variations like that, with added spaces before or after =. 因此,基本上,我需要获取所有字符串,但是当存在类似的变体时,在=之前或之后添加空格,我遇到了一个问题。 I'm sure this is really simple, but I'm really bad at regex so I would appreciate your help. 我敢肯定这确实很简单,但是我对regex真的很不好,因此,感谢您的帮助。 Thank you in advance for your answer. 预先感谢您的答复。

I do not know about C# explicitely, however something like the following should work: 我不明确地了解C#,但是应该可以进行以下操作:

(\w+)\s*=\s*"(.*?)"

(\\w+) matches a block of alphanumeric characters an creates a capturing group (\\w+)匹配一个字母数字字符块并创建一个捕获组

\\s* matches 0 or more spacing characters \\s*匹配0个或多个空格字符

= matches = =比赛=

\\s* matches 0 or more spacing characters \\s*匹配0个或多个空格字符

"(.*?)" matches "anything" and creates a capturing group over what's inside "" "(.*?)"“ anything "(.*?)"匹配,并在“”内部创建捕获组

I guess you need to make sure the global flag is enabled to get all matches. 我猜您需要确保启用了全局标志才能获取所有匹配项。

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

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