Regular exp

  • Thread starter Thread starter Tunga Torgal
  • Start date Start date
T

Tunga Torgal

Hi all,

I want to parse a string like this :
ali veli "49 50" "naber lan" deneme

It should return this substrings:
1. ali
2. veli
3. 49 50
4. naber lan
5. deneme

Actually, it is a problem of parsing quotation marks...
For this, I use this regex : (?<=").+(?=")|[^\s"]+
But, it returns
1. ali
2. veli
3. 49 50" "naber lan
4. deneme

How can I solve this problem...
thanks in advance...
 
Tunga said:
I want to parse a string like this :
ali veli "49 50" "naber lan" deneme
It should return this substrings:
1. ali 2. veli 3. 49 50 4. naber lan 5. deneme

For this, I use this regex : (?<=").+(?=")|[^\s"]+
But, it returns
1. ali 2. veli 3. 49 50" "naber lan 4. deneme

Your problem is quite simple. Regular expressions are "greedy", which means
they will try to match as many characters as possible. In this case, it
matched the largest string it could find which fits ".+", which happens to
be "49 50" "naber lan". The simplest fix is to exclude the quote character
from your any-character dot. You can also use nongreedy regular
expressions. You can read all about greedy versus non-greedy in the context
of Perl here:

http://www.developer.com/lang/article.php/10924_3330231_3

The ? syntax for nongreedy quantifiers works in .NET as well. I hope this
helps.
 
Use that code :
Regex regex=new Regex(@"""(?<param>[^""]*)"" *|(?<param>[^ ]+) *");
MatchCollection matches=regex.Matches(@"ali veli ""49 50"" ""naber lan""
deneme");
foreach(Match m in matches)
MessageBox.Show(m.Groups["param"].ToString());

Hope it helps,

Ludovic SOEUR.
 
Back
Top