top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to split string using Regex in C#?

0 votes
262 views
How to split string using Regex in C#?
posted May 9, 2017 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
 string subjectString = @"green,""yellow,green"",white,orange,""blue,black""";
        try
        {
            Regex regexObj = new Regex(@"(?<="")\b[a-z,]+\b(?="")|[a-z]+", RegexOptions.IgnoreCase);
            Match matchResults = regexObj.Match(subjectString);
            while (matchResults.Success)
            {
                Console.WriteLine("{0}", matchResults.Value);
                // matched text: matchResults.Value
                // match start: matchResults.Index
                // match length: matchResults.Length
                matchResults = matchResults.NextMatch();
            }

Output :

green
yellow,green
white
orange
blue,black

Explanation :

@"
             # Match either the regular expression below (attempting the next alternative only if this one fails)
   (?<=         # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
      ""            # Match the character “""” literally
   )
   \b           # Assert position at a word boundary
   [a-z,]       # Match a single character present in the list below
                   # A character in the range between “a” and “z”
                   # The character “,”
      +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   \b           # Assert position at a word boundary
   (?=          # Assert that the regex below can be matched, starting at this position (positive lookahead)
      ""            # Match the character “""” literally
   )
|            # Or match regular expression number 2 below (the entire match attempt fails if this one fails to match)
   [a-z]        # Match a single character in the range between “a” and “z”
      +            # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"
answer May 11, 2017 by Shivaranjini
Similar Questions
+1 vote

I have about 500 + ip regex in my apache file like this

99.45.3[2-9]..*$ 
^99.45.4[0-9]..*$ 
^99.45.5[0-9]..*$ 

I have to convert like this into CIDR .

99.45.32.0/21
99.45.40.0/21
99.45.48.0/23
99.45.50.0/21
99.45.58.0/23

Is there a module that can help me convert ip regex to ip range .

...