|
List Info
Thread: Is there a simpler/more elegant regex equivalent to this?
|
|
| Is there a simpler/more elegant regex
equivalent to this? |

|
2007-01-04 21:40:35 |
Hi all,
I'm new to regex, and hacked this one together. It seems
awfully
redundant to me, but it does have the virtue at least of
wearing its
meaning on its sleeve.
The task:
(1) match all quoted-comma'd numbers consisting of either 2
or 3
"sections". That is, critters of either form:
" "ddd,ddd,ddd" "
or
" "ddd,ddd" "
(2) Capture all of the digits, leaving the quotes and commas
for the
garbage man. For example:
" "123, 456, 789" "
should in some fashion capture
"123456789"
Here's the regex I came up with:
(?<whole>"(?<one>d{1,3}),(?<two>d{
1,3}),(?<three>d{1,3})"|"(?<one>d{
1,3}),(?<two>d{1,3})")
This is for .NET, so in the code there are actually several
more
backslashes (C# string escape char).
This works fine for me, and getting the desired
"clean" number is a
triviality.
But I get the feeling that this is the regex-equivalent of
baby-talk.
I'd like to know if there's a simpler, more elegant regex
matching the
same class of strings, and capturing essentially the same
substrings.
Thanks for any insights,
cdj
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Is there a simpler/more elegant regex
equivalent to this? |

|
2007-01-05 13:17:10 |
What does your input string look like?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Is there a simpler/more elegant regex
equivalent to this? |

|
2007-01-05 16:33:03 |
Hi!
Representative input string example:
Mar
2006,6.02,217.2,"1,579,222",33.5,"137,519&quo
t;,723.5606,66.9,18.6,17.6,19.2,12.9,12.9,,
Basically, I need to un-pretty-print csv files.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Is there a simpler/more elegant regex
equivalent to this? |

|
2007-01-05 21:26:43 |
if i understood your spec right, u need to transform the
orig input:
[it always helps when u ask a Q clearly]
2006,6.02,217.2,"1,579,222",33.5,"137,519&quo
t;,723.5606,66.9,18.6,17.6,19.2,12.9,12.9,,
to
2006,6.02,217.2,1579222,33.5,137519,723.5606,66.9,18.6,17.6,
19.2,12.9,12.9,,
if the above is true, then this C# code would accomplish
this:
using System;
using System.Text.RegularExpressions;
using System.Diagnostics;
class Regex_Replace
{
static string MyReplace(Match m)
{
// will get rid of commas in a number
string x = m.Groups[2].ToString();
x = x.Replace(",", "");
return x;
}
static void Main()
{
//input string
string OrigText =
"2006,6.02,217.2,"1,579,222",33.5,"13
7,519",723.5606,66.9,18.6,17.6,19.2,12.9,12.9,,
";
//declare regex pattern
string pattern = "(x22)([^x22]*?)(x22)";
//using STATIC Replace method, Match is passed to
Delegate
Function Regex_Replace.MyReplace
string ModifiedText = Regex.Replace(OrigText,
pattern,
new MatchEvaluator(Regex_Replace.MyReplace),
RegexOptions.IgnoreCase | RegexOptions.Multiline);
//print result of Replace
Debug.WriteLine("ModifiedText = " +
ModifiedText);
//returns:
//ModifiedText =
2006,6.02,217.2,1579222,33.5,137519,723.5606,66.9,18.6,17.6,
19.2,12.9,12.9,,
}//end of main
}//end of class
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Is there a simpler/more elegant regex
equivalent to this? |

|
2007-01-05 21:29:49 |
somehow this got omitted at the end: add it to the code
}//end of class
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://grou
ps-beta.google.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|