开发者

c# regex required [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜