473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help with Regex

Hi!
I need to parse an input string. The string must consist of 1, 2 or 3 float
numbers
separated with blanks:

Regex r = new
Regex(@"^\s*(?<x>[0-9.]+)(\s+(?<y>[0-9.]+)(\s+(?<z>[0-9.]+))*)*\s*$",
RegexOptions.ExplicitCapture);

Match m = r.Match(intput_str);

string x, y, z;

if (m.Success)
{
x = m.Groups["x"].Value;
y = m.Groups["y"].Value;
z = m.Groups["z"].Value;
if (x != "" && y != "" && z != "")
...//all three numbers
else if (x != "" && y != "")
...//only 1st and 2nd
else if (x != "")
...//only one
}

Is my regular expression pattern valid? And how to get a number of captures?
When I check m.Captures.Count, it always returns 1, not depending on how
many numbers was entered.


Apr 19 '07 #1
1 1734
There is a problem with your regular expression. It will accept a "naked"
dot as a number, and it will accept any number of dots in a number. While a
floating point number may contain a dot with nothing after it, and with
nothing before it, it must either precede or follow a digit, and there may
only be 1 dot in it. Examples:

3.14 // good
..14 // good
3. // good
.. // bad
3.14.0 // bad

The following will work perfectly:

^\s*(?>(?:\d+\.\d+|\.\d+|\d+\.|\d+)\s*){3}\s*$

Here's the explanation, starting from the innermost part:

(?:\d+\.\d+|\.\d+|\d+\.|\d+)

A match is one of 3 possible combinations:
a. 1 or more digits followed by a dot, followed by one or more digits
b. 1 or more digits alone
c. A dot followed by 1 or more digits

The sequence of the options in the "or" portion is critical, to avoid having
a dot match more than once. The nature of regular expressions is sequential
and consuming. That is, each option will be checked, and if a match is
found, the following options will not be evaluated, and the contents of the
match are "consumed," meaning that they will not be re-evaluated (except in
the case of back-tracking, which I'm coming to). Therefore, any floating
point beginning with 1 or more digits will match the entire number. However,
if there is no dot, the entire number will match next. However, if the
number begins with a dot, the entire number will match. So, the first option
rules out the second, and the second rules out the third, ensuring only 1
dot in a match. There is no quantifier for this, so only one combination
will form a match.

This is followed by \s* indicating that 0 or more spaces may follow. The \s*
is outside the grouping, due to the "or"-ing of the combinations preceding
it.

Now for the advanced part:

(?>(?:\d+\.\d+|\.\d+|\d+\.|\d+)\s*){3}

The (?>) indicates that enclosed group is "atomic," which prevents
back-tracking. Once the match is found, the regular expression engine is
prevented from back-tracking into the group to find another match. The {3}
indicates that the group must be repeated exactly 3 times. And of course you
recognize the start and end of the expression, which indicates that it must
begin and end at the beginning and end of the text, and may have spaces
before or after.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"TumurS" <sp********@magicnet.mnwrote in message
news:eD**************@TK2MSFTNGP02.phx.gbl...
Hi!
I need to parse an input string. The string must consist of 1, 2 or 3
float numbers
separated with blanks:

Regex r = new
Regex(@"^\s*(?<x>[0-9.]+)(\s+(?<y>[0-9.]+)(\s+(?<z>[0-9.]+))*)*\s*$",
RegexOptions.ExplicitCapture);

Match m = r.Match(intput_str);

string x, y, z;

if (m.Success)
{
x = m.Groups["x"].Value;
y = m.Groups["y"].Value;
z = m.Groups["z"].Value;
if (x != "" && y != "" && z != "")
...//all three numbers
else if (x != "" && y != "")
...//only 1st and 2nd
else if (x != "")
...//only one
}

Is my regular expression pattern valid? And how to get a number of
captures?
When I check m.Captures.Count, it always returns 1, not depending on how
many numbers was entered.


Apr 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony C | last post by:
I'm writing a python program which uses regular expressions, but I'm totally new to regexps. I've got Kuchling's "Regexp HOWTO", "Mastering Regular Expresions" by Oreilly, and have access to...
8
by: Bibe | last post by:
I've been trying to get this going for awhile now, and need help. I've done a regex object, and when I use IsMatch, it's behavior is quite weird. I am trying to use Regex to make sure that a...
4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
4
by: henrik | last post by:
Hi I have a regex question. I want to find all content of a <td class="someclass"> tag. This means the expression should include all other tags included between <td class="someclass"> and </td>....
9
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat...
2
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the...
3
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a...
10
by: supercrossking | last post by:
I am trying to the values of string of text in the sample before. The ds are for digits and s is for string and string of text is for a string with more than one or two values. I am trying to use...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.