473,397 Members | 1,985 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.

Regular expression : Grouping decimal values and double quote

Hi all,

I have a problem constructing a regular expression using .net.

I have a string, separated with comma, and I want to group the string
together but, I failed to group a numeric character with decimal values.

Example string : 1, 2.3, "two"," three"

So, I want to group this string into 4 groups (1), (2.3), (two) and (three)

The best regular expression that I have so far is:
(?:^|\s*\,\s*)((?:"(?<SubString>(?:""|[^"])*)")+)|((?<SubString>(\d))+)

But this regex will return (1), (2), (3), (two) and (three).

So, what is the right regular expression to do this? Please help.

Thanks.


Nov 15 '05 #1
8 3359
Will String.Split(",") do ?

<Kalpesh/>

Nov 15 '05 #2
No. Consider this string:
string s = "1, 2.3, \"ab,\"\"c\", \"e.ff,;$\"";

I want to split it into (1), (2.3), (ab,""c) and (e.ff,;$).

Got my point? Please help.
"Kalpesh Shah" <ka*****@disc.microsoft.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
Will String.Split(",") do ?

<Kalpesh/>

Nov 15 '05 #3
Hello

Try this expression
(?:^|\s*\,\s*)(?:(?:"(?<SubString>(?:""|[^"])*)"\s*)|(?:\s*(?<SubString>(?:\
s*[^\s,]+)*)\s*))

Best regards
Sherif

"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
Hi all,

I have a problem constructing a regular expression using .net.

I have a string, separated with comma, and I want to group the string
together but, I failed to group a numeric character with decimal values.

Example string : 1, 2.3, "two"," three"

So, I want to group this string into 4 groups (1), (2.3), (two) and (three)
The best regular expression that I have so far is:
(?:^|\s*\,\s*)((?:"(?<SubString>(?:""|[^"])*)")+)|((?<SubString>(\d))+)

But this regex will return (1), (2), (3), (two) and (three).

So, what is the right regular expression to do this? Please help.

Thanks.

Nov 15 '05 #4
Thanx a lot Metainy!

It works. But that regex also matches invalid decimal values. Like, it still
match 1.23aa value. And same case also happened to the double-quote
character, of which I wanted it to start and end with double-quote, with no
trailing character except a comma or no char at all. Got my point?

Just to get it clear:
- 1.23, "abc"qwe" = valid
- 1.23x, "abc"qwe" = invalid
- 1.23, "abc"qwe"xx = invalid

And one more thing is, any good (but free) resource of regex tutorial? ebook
or website.

Still hoping for assistance here.

Thank you.


Nov 15 '05 #5
Hello Ahmad

Try this one
^(?:(?:(?:^|,)\s*)(?:(?:"(?<SubString>(""|[^"])*)")|(?<SubString>\d+(?:\.\d+
)?))(?=\s*(?:$|,))\s*)+$

Best regards,
Sherif

"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Thanx a lot Metainy!

It works. But that regex also matches invalid decimal values. Like, it still match 1.23aa value. And same case also happened to the double-quote
character, of which I wanted it to start and end with double-quote, with no trailing character except a comma or no char at all. Got my point?

Just to get it clear:
- 1.23, "abc"qwe" = valid
- 1.23x, "abc"qwe" = invalid
- 1.23, "abc"qwe"xx = invalid

And one more thing is, any good (but free) resource of regex tutorial? ebook or website.

Still hoping for assistance here.

Thank you.

Nov 15 '05 #6
Hi ElMetainy,

That one does work, but I also need the double-quote character to be in
between the double quote.
Llike my previous post:

1.23, "abc"qwe" = valid (and by using MatchCollection on <SubString>, this
will return [1.23] and [abc"qwe])
1.23, "abc"qwe"xx = invalid
1.23xx, "abc"qwe" = invalid

Can you help me...just a little bit more? :) You almost got it right.

p/s: Sorry, I still got no time to learn regex. But I really need a quick
solution right now.

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hello Ahmad

Try this one
^(?:(?:(?:^|,)\s*)(?:(?:"(?<SubString>(""|[^"])*)")|(?<SubString>\d+(?:\.\d+ )?))(?=\s*(?:$|,))\s*)+$

Best regards,
Sherif

"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Thanx a lot Metainy!

It works. But that regex also matches invalid decimal values. Like, it

still
match 1.23aa value. And same case also happened to the double-quote
character, of which I wanted it to start and end with double-quote, with

no
trailing character except a comma or no char at all. Got my point?

Just to get it clear:
- 1.23, "abc"qwe" = valid
- 1.23x, "abc"qwe" = invalid
- 1.23, "abc"qwe"xx = invalid

And one more thing is, any good (but free) resource of regex tutorial?

ebook
or website.

Still hoping for assistance here.

Thank you.


Nov 15 '05 #7
Hello

This can be too complicated

How do I treat the double quote and comma. A ',' between double quotes is
considered a part of the string and a double quote between double quotes is
also considered a part of the string
Imagine this

1.23,"aa,ddd",123 this should match [1.23], [aa,ddd] and [123]

1.23,"abc"qwe,"ee",124 should match [1.23], [abc"qwe,"ee] and [124] or be
considered invalid??
To take this decision you have to understand the nature of the data (for
example being able to distinguish a contact's first name from his
nickname) which is not possible with regular expressions.

This is why it is difficult to match one double quote between 2 double
quotes.

Here is where the "" resolves the ambiguity
1.23,"abc""qwe,""ee",124 should match [1.23], [abc"qwe,"ee] and [124]
meaning that 2 consecutive double quotes within double quotes should be
treated as a one double quote which is a part of the string. The "" is
standard in formats like csv.
Best regards,
Sherif
"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:#n**************@TK2MSFTNGP09.phx.gbl...
Hi ElMetainy,

That one does work, but I also need the double-quote character to be in
between the double quote.
Llike my previous post:

1.23, "abc"qwe" = valid (and by using MatchCollection on <SubString>, this
will return [1.23] and [abc"qwe])
1.23, "abc"qwe"xx = invalid
1.23xx, "abc"qwe" = invalid

Can you help me...just a little bit more? :) You almost got it right.

p/s: Sorry, I still got no time to learn regex. But I really need a quick
solution right now.

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hello Ahmad

Try this one

^(?:(?:(?:^|,)\s*)(?:(?:"(?<SubString>(""|[^"])*)")|(?<SubString>\d+(?:\.\d+
)?))(?=\s*(?:$|,))\s*)+$

Best regards,
Sherif

"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
Thanx a lot Metainy!

It works. But that regex also matches invalid decimal values. Like, it

still
match 1.23aa value. And same case also happened to the double-quote
character, of which I wanted it to start and end with double-quote,
with no
trailing character except a comma or no char at all. Got my point?

Just to get it clear:
- 1.23, "abc"qwe" = valid
- 1.23x, "abc"qwe" = invalid
- 1.23, "abc"qwe"xx = invalid

And one more thing is, any good (but free) resource of regex tutorial?

ebook
or website.

Still hoping for assistance here.

Thank you.



Nov 15 '05 #8
Hi,

I know that it was too complicated, that's why I'm here.

But, I think I have my way out now. I can use MatchColelction and break the
string apart between the comma, and use another regex to validate every
broken string. :)

Anyway, you've been a great help ElMetainy. Thanks a lot.

Bye.

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:uy*************@TK2MSFTNGP11.phx.gbl...
Hello

This can be too complicated

How do I treat the double quote and comma. A ',' between double quotes is
considered a part of the string and a double quote between double quotes is also considered a part of the string
Imagine this

1.23,"aa,ddd",123 this should match [1.23], [aa,ddd] and [123]

1.23,"abc"qwe,"ee",124 should match [1.23], [abc"qwe,"ee] and [124] or be
considered invalid??
To take this decision you have to understand the nature of the data (for
example being able to distinguish a contact's first name from his
nickname) which is not possible with regular expressions.

This is why it is difficult to match one double quote between 2 double
quotes.

Here is where the "" resolves the ambiguity
1.23,"abc""qwe,""ee",124 should match [1.23], [abc"qwe,"ee] and [124]
meaning that 2 consecutive double quotes within double quotes should be
treated as a one double quote which is a part of the string. The "" is
standard in formats like csv.
Best regards,
Sherif
"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:#n**************@TK2MSFTNGP09.phx.gbl...
Hi ElMetainy,

That one does work, but I also need the double-quote character to be in
between the double quote.
Llike my previous post:

1.23, "abc"qwe" = valid (and by using MatchCollection on <SubString>, this
will return [1.23] and [abc"qwe])
1.23, "abc"qwe"xx = invalid
1.23xx, "abc"qwe" = invalid

Can you help me...just a little bit more? :) You almost got it right.

p/s: Sorry, I still got no time to learn regex. But I really need a quick solution right now.

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hello Ahmad

Try this one

^(?:(?:(?:^|,)\s*)(?:(?:"(?<SubString>(""|[^"])*)")|(?<SubString>\d+(?:\.\d+
)?))(?=\s*(?:$|,))\s*)+$

Best regards,
Sherif

"Ahmad A. Rahman" <je************@yahoo.com> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
> Thanx a lot Metainy!
>
> It works. But that regex also matches invalid decimal values. Like, it still
> match 1.23aa value. And same case also happened to the double-quote
> character, of which I wanted it to start and end with double-quote, with no
> trailing character except a comma or no char at all. Got my point?
>
> Just to get it clear:
> - 1.23, "abc"qwe" = valid
> - 1.23x, "abc"qwe" = invalid
> - 1.23, "abc"qwe"xx = invalid
>
> And one more thing is, any good (but free) resource of regex tutorial? ebook
> or website.
>
> Still hoping for assistance here.
>
> Thank you.
>
>
>
>



Nov 15 '05 #9

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

Similar topics

16
by: Sims | last post by:
Hi, I need some help to split data using regular expression Consider the string '1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly get =1, =2,=3. Now if i have
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: bas jaburg | last post by:
Hi, I need to know the regular expression that checks the whether a 1-decimal number has been entered. Also it needs to ok if no trailing number before the decimal dot has been entered So:...
3
by: Robert Scheer | last post by:
Hi. I have a regularexpression validator control on a page. This regular expression validates a textbox to accept only numbers and commas: validationexpression="*" I am trying to modify this...
7
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus()...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
10
by: Mike9900 | last post by:
Hello, I need a regular expression to match a currency with its symbol, for example Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound return -66.99 or any other...
7
by: hellbent4u | last post by:
Hi All, I need a regular expression to be used in an ASP page which would allow a user to enter: 1000 as well as 1,000 i.e. an amount without comma as well as with comma(for example 10,000...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.