tags:

views:

255

answers:

2

I am not very good at regular expression but want to do some thing like this :

string="c test123 d split"

I want to split the word based on "c" and "d". this can be any word which i already have. The string will be given by the user. i want "test123" and "split" as my output. and there can be any number of words i.e "c test123 d split e new" etc. c d e i have already with me. I want just the next word after that word i.e after c i have test123 and after d i have split and after e i have new so i need test123 and split and new. how can I do this??? And one more thing I will pass just c first than d and than e. not together all of them. I tried

string strSearchWord="c ";
Regex testRegex1 = new Regex(strSearchWord); List lstValues = testRegex1.Split("c test123 d split").ToList();

But it's working only for last character i.e for d it's giving the last word but for c it includes test123 d split.

How shall I do this???

The input might be

string strSearchWord="c mytest1 d newtest1 e lasttest1"; split should be based on characters "c d and e". I will pass them one by one.

or

string strSearchword="q 100 p 200 t 2000"; split should be based on characters "q p and t". I will pass them one by one.

or string strSearchWord="t 100 r pass"; split should be based on characters "t r". I will pass them one by one.

or

string strSeaRCHwORD="fi 100 se 2000 td 500 ft 200 fv 6000 lt thanks "; split should be based on characters "fi,se,td,ft,fv and lt". I will pass them one by one.

Hope it's clear. Any other specification????

+1  A: 
string[] splitArray = null;
splitArray = Regex.Split(subjectString, @"\s*\b(c|d)\b\s*");

will split the string along the "words" c or d, whether or not they are surrounded by whitespace, but only if they occur as entire words (therefore the \b word boundary anchors).

This gives you all the substrings between your words as an array.

If you want to loop through the string manually, picking out each word after the search words one by one, you could use positive lookbehind:

string resultString = null;
resultString = Regex.Match(subjectString, @"(?<=\bc\b\s*)\w+").Value;

will find the word after c. Do the same for d ((?<=\bd\b\s*)\w+) etc.

This regex means:

(?<=\bc\b\s*): Assert that it is possible to match the "complete word" c, optionally followed by space characters, to the left of the current position in the string (positive lookbehind).

\w+: Then match any alphanumeric characters (including _) that follow.

Tim Pietzcker
It's inside the loop so I have c first than i have d i don't have all the words together. i.e the split word I don't have together.
Jankhana
This is good. But OP has not clearly defined the boundaries. So, your answer may be acceptable. The above pattern will not work on "gggg d ppp c test123 d split" and "ggggd pppc test123d split". Well, since the requirement is not clear, you're safe =)
Nayan
@Jankhana: Then use the second option. You can build the regex as `@"(?<=\b" + searchString + @"\b\s*)\w+"`
Tim Pietzcker
Hey that's working with sample. Will try and let you know the result soon. Thanks.
Jankhana
what abt if i have split character as , or some thing else i.e instead of "c test123 d split" I have "c,test123,d,split". That also is working with sample if I include that with my search string. Thankssssssssssss
Jankhana
Depends on if there is whitespace around the new separation characters. On this string, you could use `(?<=\bc\b,)\w+` for example or `(?<=\bc\s*,\s*)\w+` if whitespace is allowed.
Tim Pietzcker
@Tim:Would tell for what we are using this: "(?<=". That is the use of this symbol????
Jankhana
Have edited my answer. Try www.regular-expressions.info for more details. So, have you solved your problem now?
Tim Pietzcker
s I have solved my problem. Thanks
Jankhana
@Jankhana: How about voting on some of the answers and accepting one, then?
Tim Pietzcker
I am not able to accept the answer don't know why!!!! Hmmm i have up voted your comment also. hmmm will try to accept and increase the votes. Sorry:-(
Jankhana
@Tim: I accepted your answer I was able to do that now so. Anyways Thanks.
Jankhana
A: 

use regex groups.

the regex would be

"c(.+?)d(.+?)"

and you would retrieve it as

Regex r = new Regex("c\s(.+?)\sd\s(.+?)"); // \s is whitespace
r.Match("c test123 d split").Groups[1] //is the 1st group "test123"
r.Match("c test123 d split").Groups[2] //is the 2nd group "split"
r.Match("c test123 d split").Groups[0] //is the whole match "c test123 d split
Axarydax
This assume that c is followed by d. Not a good solution.
Nayan
Hmmm I have only c or d or e i don't have them together!!!!
Jankhana