473,569 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)( (?:"(?<SubStrin g>(?:""|[^"])*)")+)|((?<Sub String>(\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 3376
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.m icrosoft.com> wrote in message
news:OV******** ******@TK2MSFTN GP10.phx.gbl...
Will String.Split(", ") do ?

<Kalpesh/>

Nov 15 '05 #3
Hello

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

Best regards
Sherif

"Ahmad A. Rahman" <je************ @yahoo.com> wrote in message
news:uG******** ******@tk2msftn gp13.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*)( (?:"(?<SubStrin g>(?:""|[^"])*)")+)|((?<Sub String>(\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*)(?:(?:"(?<Su bString>(""|[^"])*)")|(?<SubStr ing>\d+(?:\.\d+
)?))(?=\s*(?:$| ,))\s*)+$

Best regards,
Sherif

"Ahmad A. Rahman" <je************ @yahoo.com> wrote in message
news:Ou******** ******@TK2MSFTN GP10.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.NO SPAM> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hello Ahmad

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

Best regards,
Sherif

"Ahmad A. Rahman" <je************ @yahoo.com> wrote in message
news:Ou******** ******@TK2MSFTN GP10.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",1 23 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******** ******@TK2MSFTN GP09.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.NO SPAM> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hello Ahmad

Try this one

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

Best regards,
Sherif

"Ahmad A. Rahman" <je************ @yahoo.com> wrote in message
news:Ou******** ******@TK2MSFTN GP10.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.NO SPAM> wrote in message
news:uy******** *****@TK2MSFTNG P11.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",1 23 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******** ******@TK2MSFTN GP09.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.NO SPAM> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hello Ahmad

Try this one

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

Best regards,
Sherif

"Ahmad A. Rahman" <je************ @yahoo.com> wrote in message
news:Ou******** ******@TK2MSFTN GP10.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
3536
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
6117
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to...
5
13646
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: 1.3 --> ok 1.0 --> ok .1 --> ok
3
7624
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 expression to not allow commas at the beginning and at the end of the expression without success. It needs to allow commas only between the numbers....
7
12926
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() field.hpage.select() return false
5
2273
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 each pharse? test case: "I love" all "of you" I would like it return:
11
3088
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 a hand? import regsub
10
10179
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 combination return the decimal number. I have created a regular expression, but it seems that it does not work if the number is Pound66.99 but it works...
7
4393
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 or 100,000 etc.)
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.