I am very poor in regex, i would appreciate if someone helps in this regex
Regex should match :- any text before first forward-slash else full text, and then text that is not inside braces
HELLO/anything/blah/bhah ---------> should ret开发者_Python百科urn HELLO
{HELLO}/anything/blah/bhah -------> should not return any thing
ABC/blah/blah---------------------> should return ABC
ABC-------------------------------> should return ABC
^\w+
should work where:
^
- match beginning of input
\w
- match any word character (0-9a-zA-Z plus a few punctuation)
+
- match one or more
If you'd prefer to be explicit:
^[0-9a-zA-Z]+
[0-9a-zA-Z]
means almost the same as \w
This simple regex matches the requirements as you stated them:
^[A-Z]*
Starting from the start of the string, it takes any uppercase letters until it meets a character not in the list, which means it stops at the {
and /
characters.
You can use this regex for example:
var l = new string[] {
"HELLO/anything/blah/bhah",
"{HELLO}/anything/blah/bhah",
"ABC/blah/blah",
"ABC"
};
foreach (var s in l)
{
Regex r = new Regex("^(?!{[^/]*})([^/]*)/?");
Console.WriteLine(r.Match(s).Groups[1].Value);
}
精彩评论