开发者

Extracting one word based on special character using Regular Expression in C#

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 n开发者_JS百科ew. 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????


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.


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜