Help Required in Regular Expression

57 views
Skip to first unread message

Ruchi Saini

unread,
May 26, 2015, 12:38:34 PM5/26/15
to re...@googlegroups.com
Hi,

Can you please advice me on following regular expressions

1)(green|blue)?+.+
i understood that it will take either green or blue which is optional but +.+
changes its meaning and it accepts colors which are not green and blue
 but i dont understand how is it affecting

2)^([\"']?)\\d\\d:\\d\\d\\1,([\"']?)[A-Z]\\w+\\2,.*$"
It accepts
10:23,Hello,World,7432e01
10:53,"Hello","World","7432e01"
10:23,Hello,,queue 2,7432e01
i believe backreferences are used here then they shd be using
value in capturing group only , why are these two value accepeted -> Hello,World.
Also if you check the third line, if i dont give any value after Hello, then that is also accepted
Please guide

Thanks
Ruchi

iiz

unread,
May 27, 2015, 3:02:56 AM5/27/15
to re...@googlegroups.com
Hi Ruchi,

These are two questions.
1) You are saying green and blue are optional by inserting the question mark ? which is acting as a quantifier meaning zero or one time. Then you mark a plus which means 1 or more times. The effect is the same as writing * qhich signifies any number of times (zero or more). 

Then you write .+ which means one or more times any character. The end result would be your are matching any string consisting of one or more characters. From the regex I do not know what you are trying to match, but semantically your regexd is exactly equal to just writing .+ with the difference that if the string starts with green or blue you now have it in a capturing group.


2) 
You are matching a time that is surrounded by either single or double or no quotes, followed by a word that starts with a capital and is surrounded by either  single or double or no quotes. After the komma it will match anything (because of .*) which is why after the time and word anything is matched, such as ,"World"

The two backreferences \1 and \2 makes sure the quotes are matched by the same time, so a start double quote is not matched by an ending double quote.


Op dinsdag 26 mei 2015 18:38:34 UTC+2 schreef Ruchi Saini:
Reply all
Reply to author
Forward
0 new messages