简体   繁体   English

获取2个字符之间的字符串正则表达式

[英]Get string between 2 characters Regex

I am new to regex and it really confuses me. 我是正则表达式的新手,这确实使我感到困惑。 What I am trying to accomplish is finding the string between 2 specified characters where the string should contain another specified character within it. 我要完成的工作是找到2个指定字符之间的字符串,其中字符串应在其中包含另一个指定字符。

String Example: 'Help--Me' 字符串示例:“帮助-我”

In this case I would be looking for the string Help Me that's between the two apostrophes and contains -- in it. 在这种情况下,我将寻找字符串“ Help Me”,该字符串在两个撇号之间并且包含-。

The Regex I have currently is @"(?<=\\')(--.*?)(?=\\')" This seems to only work if the -- is at the beginning of the string Example: '--HelpMe' 我当前使用的正则表达式为@"(?<=\\')(--.*?)(?=\\')"这似乎仅在-位于字符串开头时才有效。帮我'

Thanks in advance 提前致谢

You're very close—nice attempt. 您非常亲密-不错的尝试。 You need another wildcard string at the beginning: 开头需要另一个通配符字符串:

@"(?<=\')(.*?--.*?)(?=\')"

This way, it'll look for a string of any characters following the ' (the minimum string, by the way, due to the non-greedy quantifier, *? ), a -- , another string of any characters (again, the minimum string), and finally the closing ' . 通过这种方式,它会寻找以下任何字符的字符串'最小的字符串,顺便说一句,由于非贪婪量词, *?一个--的任何字母组成的字符串(同样,最小字符串),最后是结束符'

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

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