简体   繁体   中英

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.

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.

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 ' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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