开发者

c# regex required [closed]

开发者 https://www.devze.com 2023-03-19 16:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
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

精彩评论

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