开发者

grouping regex help

开发者 https://www.devze.com 2023-03-15 05:21 出处:网络
I have this regex for defining identifiers: [\\w|@|#|_][\\w|\\.|\\$|@|#|_]* I need to allow as well groups of identifier that are grouped using [group] or \"group\" and for allowing \"s inside the

I have this regex for defining identifiers:

[\w|@|#|_][\w|\.|\$|@|#|_]*

I need to allow as well groups of identifier that are grouped using [group] or "group" and for allowing "s inside the "group" you would need to write "" (two) and the same for the [group] you would do ]] for one ].

the group might contain anything allowed in identifiers, spaces and any of these characters: tilde (~) hyphen (-) exclamation point (!) left brace ({) percent (%) right brace (}) c开发者_运维技巧aret (^) apostrophe (') ampersand (&) period (.) left parenthesis (() backslash () right parenthesis ()) accent grave (`)

Examples :

"asda$@.asd ' a12876 ]] "" " => asda$@.asd ' a12876 ]] " 

[asda$@.asd ' a12876 ]] "" ] => asda$@.asd ' a12876 ] "" 


You don't need any of the | in your [character classes] because it causes any character to match. (I am assuming you don't want identifiers to begin with |, for example.

string mystring = "[asda$@.asd ' a12876 ]] \"\" ]";
Console.WriteLine(mystring);
MatchCollection matches = 
   Regex.Matches(mystring,
                 @"[\w@#](?:[\w\.\$@#])*|\[[\w@#](?:\[\[|\]\]|[""\w\s\.\$@#'])*\]|""[\w@#](?:\""\""|['\s\[\w\.\$@#\]])*""",
                 RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
foreach (Match match in matches)
{
   string id = match.Value;
   // The first character of the match tells us which escape sequence to use
   // for the replacement.
   if (match.Value[0] == '[')
      id = id.Substring (1, id.Length - 2).Replace ("[[", "[").Replace ("]]", "]");
   else if (match.Value[0] == '"')
      id = id.Substring (1, id.Length - 2).Replace ("\"\"", "\"");
   Console.WriteLine (id);
}
0

精彩评论

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