getting arguments withn RegEx please help

M

matro

hi there,

I'm using RegEx object with the string "(\[.*?\])" to get arguments
from a line like this:

This line has got [one] argument and [another] one.

the issue is that I obtain a MathCollection with "[one]" and
"[another]".

how can I modify the pattern string to have [ and ] stripped so to get
"one" and "another"?

please help!
thank you.

--------

Francesco "Matro" Martire

available at www.realpopup.it:
RealPopup, the freeware winpopup replacer
RealAccount, freeware plugin for MS Outlook XP
MaTreo, freeware frontscreen for your Treo and PalmOS devices
 
J

Jhon

Hi,

You can do this with one match, like:

using System.Text.RegularExpressions;

Match m =
Regex.Match (strInput, "(?:.*?\\[(?'key'\\w*?)\\])*" );

// print all keys in string that are between [ ]
foreach (Capture c in m.Groups["key"].Captures)
{
System.Console.Write (c.ToString() + "\r\n" );
}

HTH
greetings
 
M

Mark Coffman

Jhon already answered this but instead of writing out the code I thought you
should know what is happening here. First, you're getting "[one]" instead
of "one" because your parentheses are requesting the brackets. If your
regexp was "\[(.*?)\]" you'd trim that off.

Also, the "." matches any character except newline, so if you only want a-z,
you could use \w, otherwise you might match spaces between those brackets,
which may or may not be what you want.

The "?" at the end tells the regular expression to stop as soon as it can
and still match, so it won't match the right bracket as part of the "."
match.
 
M

matro

should know what is happening here. First, you're getting "[one]" instead
of "one" because your parentheses are requesting the brackets. If your
regexp was "\[(.*?)\]" you'd trim that off.

thank you both! this solved my issue. :) thanks for explanations too,
really appreciated.

--------

Francesco "Matro" Martire

available at www.realpopup.it:
RealPopup, the freeware winpopup replacer
RealAccount, freeware plugin for MS Outlook XP
MaTreo, freeware frontscreen for your Treo and PalmOS devices
 

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