tags:

views:

73

answers:

8

Hello All Request your valueable time in helping me to find substring values using Regular Expressions..

This is my input

"ADMIN:YESEXT:NOBAT:NOBOM:NO"

I should be able to extract the following things to output

ADMIN:YES
EXT:NO
BAT:NO
BOM:NO

I used the following regex to get just the ADMIN: string ^\s?(?:ADMIN:) How should i modify the regex expression to get the required output

+1  A: 

Do you know that the value following the colon will always be YES or NO? I'd say, you want something like

/([^:]+):(YES|NO)/g

Where $1 is the property and $2 its value.

David Hedlund
The question is tagged as C# and this looks to be a javascript regular expression from formatting and the suffix of a global flag
rrrr
well yes, i'll have to admit to not having noticed the tags. i just treated it as a regex question, and answered it as such. i'd argue that this is more of a question of regex strategy than syntax
David Hedlund
+1  A: 

You could try matching this regex:

(.*?):(YES|NO)
soulmerge
Nice. This regexp uses a lazy quantifer ( the question mark after .* ). to locate the correct colon character. But this regexp has a small mistake. The asterix (*) has to applied on the whole expression and not only to (YES|NO). So this would become: "((.*?):(YES|NO))*"
ardsrk
+1  A: 

([^:]+):(YES|NO) will do. This will match all text followed by :YES or :NO

Ikke
+2  A: 

I would use:

(\w+):(YES:NO)
cletus
A: 
(ADMIN|EXT|BAT|BOM):(YES|NO)
Jesper Palm
A: 

If you want to get the values out in an explicit order, then you can use:

^\s?(?:ADMIN:(YES|NO))(?:EXT:(YES|NO))(?:BAT:(YES|NO))(?:BOM:(YES|NO))
Paul Manzotti
A: 

I would go for

\w*:(NO|YES)

\w*      - match as many word-charachters as possible  
:        - match a colon  
(NO|YES) - match YES or NO

To get all results, you can use something like:

Regex regexObj = new Regex(@"\w*:(NO|YES)");
Match matchResults = regexObj.Match(theStringToSearch);
while (matchResults.Success) {
   //take us of matchResults....

   matchResults = matchResults.NextMatch();
}
Vegar
A: 

The regexp suggested by @soulmerge uses a lazy quantifer ( the question mark after .* ). to locate the correct colon character. But this regexp has a small mistake. The asterix (*) has to be applied on the whole expression and not only to (YES|NO).

So this would become:

((.*?):(YES|NO))*

I tried to comment to his answer but the regexp was not getting displayed properly.

ardsrk
That will match the whole string in one go, but @soulmerge's regex (like most of the others) is meant for iterating through the tokens via the `Matches()` method or repeated calls to `Match()`. By the way, you can use backticks to format code, both in posts and in comments.
Alan Moore
Oh ok. Got it. I have been making such mistakes all over SO lately. Thankfully SO users have been lenient enough not to downvote my answers but only point out my mistakes. Thanks.
ardsrk