By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

getting arguments withn RegEx please help

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"matro" <ma***@despammed.com> wrote in message
news:ct********************************@4ax.com...
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

Nov 15 '05 #2

P: n/a
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.

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"?

Nov 15 '05 #3

P: n/a
On Fri, 17 Oct 2003 15:29:40 -0400, "Mark Coffman" <mc******@kua.com>
wrote:
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
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.