tags:

views:

166

answers:

4

hi folks.,

i have a pattern as like this

"The world is #bright# and #beautiful#"

i need to retrieve the string "bright","beautiful" inside # # .. any pointers


My solution (thanks to Bolu):

string s = "The world is #bright# and #beautiful#";
    string[] str = s.Split('#');
    for (int i = 0; i <= str.Length - 1; i++)
    {
        if (i % 2 != 0)
        {
            Response.Write(str[i] + "<br />");
        }
    }
+4  A: 

As long as you can't have nested #...# sequences, #([^#]+)# will work, and will capture the content between #'s as the first backreference.

Explanation:

#        match a literal # character
(        open a capturing group
  [^     open a negated character class
     #   don't match # (since the character class is negated)
  ]+     close the class, match it one or more times
)        close the capturing group
#        match a literal # character
Daniel Vandersluis
I'm genuinely confused. Where does the OP say he has nested sequences? Or were you just highlighting this as a potential problem.
annakata
@annakata just highlighting it as a issue that would break the regex.
Daniel Vandersluis
@annakata - he is highlighting it as a potential problem. Regular expressions are a sound mathematical thing. If you grok them, you are probably mathematically sound and prone to worrying about potential problems. This is a good thing, but quite often, you can get the job done far quicker without worrying ;)
Daren Thomas
This does not capture the string __inside__ the `#..#` but captures the entire match with the `#`'s still there.
Callum Rogers
@Callum I added a capturing group.
Daniel Vandersluis
+3  A: 

Check out the Match object:

var match = Regex.Match(yourstring, @"The world is #(.*)# and beautiful")
var bright = match.Groups[1]

Of course this breaks down when you have more than two #'s in your string. Then you probably want to do a non-greedy match. This can be done with the regex "#(.*?)#". This will match the shortest string between two sharps and still have the contents in the first group.

Daren Thomas
+1 But although non-greedy is good, I'd caution against thinking it solves much - false sense of security when you consider nested and unclosed sequences, although of course the OP opens himself up to these by just having the single delimiter I suppose.
annakata
Sometimes systems like these don't have to be totally sound - say you only ever have an "identifier" between the sharps. You could then use `#([a-zA-Z0-9_]+)#` and be over and done with it. I've used stuff like this for simple macro systems before - if you control the input, not much can go wrong...
Daren Thomas
+2  A: 

You need to set up a Capturing Group by wrapping the part you want to capture in round brackets () and optionally specifying a name for the capture:

Regex r = new Regex(@"#([^#]+?)#");

which can be accessed by using this code:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups[1];

Or with a named parameter:

Regex r = new Regex(@"#(?<mycapture>[^#]+?)#");

which can be accessed by using this code:

Match m = r.Match("The world is #bright# and beautiful");
string capture = m.Groups["mycapture"];
Callum Rogers
+6  A: 

If all you want is the string inside ##, then no need for regex, just use string.Split:

string rawstring="The world is #bright# and beautiful";
string[] tem=rawstring.Split('#');

After that, all you need is to get the even item (with index: 1,3,5....) from the string[] tem

Bolu
i have marked yours as answer ;-)
Asif khan