Regular Expression Help

D

Dan

Hi. I'm new to regular expressions, and have what I hope is a basic
question.

In the file that my code is parsing, some lines may look like

:name:colour
:name:colour

and some lines may just have

:name

Note that the name and colour would actually be a name and a colour - not
the text "name" and "colour".

The regular expression below works for the case where there's both the name
and the colour, but not where there's just the name. How do I make it so
that the second parameter is optional?

Thanks for any info,
Dan.


Match m = Regex.Match (buffer, @":(?<name>.*):(?<colour>.*)");

name = m.Groups ["name"].ToString ();
colour_string = m.Groups ["colour"].ToString ();
 
K

Ken Arway

Dan said:
Hi. I'm new to regular expressions, and have what I hope is a basic
question.

In the file that my code is parsing, some lines may look like

:name:colour
:name:colour

and some lines may just have

:name

Note that the name and colour would actually be a name and a colour - not
the text "name" and "colour".

The regular expression below works for the case where there's both the name
and the colour, but not where there's just the name. How do I make it so
that the second parameter is optional?

Thanks for any info,
Dan.


Match m = Regex.Match (buffer, @":(?<name>.*):(?<colour>.*)");

name = m.Groups ["name"].ToString ();
colour_string = m.Groups ["colour"].ToString ();
Regex regex = new Regex(@":(?<name>\w+)[\s|:]?(?<colour>.+)?",
(RegexOptions) 0);

Test input:
:MyRed:#ff0000
:MyBlue:#0000ff
:MyGreen

Test Output:
Matching: :MyRed:#ff0000
name =»MyRed«=
colour =»#ff0000«=

Matching: :MyBlue:#0000ff
name =»MyBlue«=
colour =»#0000ff«=

Matching: :MyGreen
name =»MyGreen«=
 
D

Dan

Hi. I'm new to regular expressions, and have what I hope is a basic
question.

In the file that my code is parsing, some lines may look like

:name:colour
:name:colour

and some lines may just have

:name

Note that the name and colour would actually be a name and a colour - not
the text "name" and "colour".

The regular expression below works for the case where there's both the name
and the colour, but not where there's just the name. How do I make it so
that the second parameter is optional?

Thanks for any info,
Dan.


Match m = Regex.Match (buffer, @":(?<name>.*):(?<colour>.*)");

name = m.Groups ["name"].ToString ();
colour_string = m.Groups ["colour"].ToString ();
Regex regex = new Regex(@":(?<name>\w+)[\s|:]?(?<colour>.+)?",
(RegexOptions) 0);

Test input:
:MyRed:#ff0000
:MyBlue:#0000ff
:MyGreen

Test Output:
Matching: :MyRed:#ff0000
name =»MyRed«=
colour =»#ff0000«=

Matching: :MyBlue:#0000ff
name =»MyBlue«=
colour =»#0000ff«=

Matching: :MyGreen
name =»MyGreen«=



Thanks for that. Just so that I understand it - does a question mark either
side of a group make it optional? Also for the [\s|:] part - does that mean
that it matches either a white space or a colon? I presume a newline's a
whitespace then? Could I also have done "?:))?" ?

Thanks again,
Dan.
 
K

Ken Arway

Dan said:
Thanks for that. Just so that I understand it - does a question mark either
side of a group make it optional?

Only after a group or entity--matches zero or one time.
Also for the [\s|:] part - does that mean
that it matches either a white space or a colon?
Yes.

I presume a newline's a
whitespace then? Could I also have done "?:))?" ?

It would work, but not quite as cleanly. The named groups would be OK,
but an additional capture group consisting of the colon is also generated.

Go to GotDotNet's User Samples page
http://www.gotdotnet.com/community/usersamples/
and search for "regex workbench". It's a great utility for creating,
playing with and saving regular expressions, and it also explains the
regex symbols.
 
L

Luc E. Mistiaen

whitespace then? Could I also have done "?:))?" ?
It would work, but not quite as cleanly. The named groups would be OK,
but an additional capture group consisting of the colon is also generated.

Make that ":?"

Especially the parenthesis that would create the group ; the leading ? was
spurious I guess or I belongs to the expression before it and not show in
your string.

/LM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top