{unknown string}
{unknown string
unknown string}
unknown string
How do I come up with a regex that recognizes just the string (which is unknown, so I cannot do an explicit match to a specific string) in all four of the above cases?
You haven't tried much, have you?
string result = Regex.Match(input, "hello").Value;
If you just want something between curly braces:
string result = Regex.Match(input, @"\{?(.*)\}?").Groups[1].Value;
\w+
It will match all "word"-characters
If you need to generalize it to something that's "between optional curly braces" you could use:
\{?(.+?)\}?
which means:
\\{?
- an optional curly brace character. It's escaped because {
has a special meaning in regular expressions. ?
quantifier means 0
or 1
times (thus optional) (.+?)
- means anything in non-greedy mode. You need non-greedy here so that regex stops right before the following }
(if any) \\}?
- the same as item #1
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.