0

I asked a very similar question to this one almost a month ago here.

I am trying very hard to understand regular expressions, but not a bit of it makes any sense. SLak's solution in that question worked well, but when I try to use the Regex Helper at http://gskinner.com/RegExr/ it only matches the first comma of -2.2,1.1-6.9,2.3-12.8,2.3 when given the regex ,|(?<!^|,)(?=-)

In other words I can't find a single regex tool that will even help me understand it. Well, enough whining. I'm now trying to re-write this regex so that I can do a Regex.Split() to split up the string 2.2 1.1-6.9,2.3-12.8 2.3 into -2.2, 1.1, -6.9, 2.3, -12.8, and 2.3.

The difference the aforementioned question is that there can now be leading and/or trailing whitespace, and that whitespace can act as a delimiter as can a comma.

I tried using \s|,|(?<!^|,)(?=-) but this doesn't work. I tried using this to split 293.46701,72.238185, but C# just tells me "the input string was not in a correct format". Please note that there is leading and trailing whitespace that SO does not display correctly.

EDIT: Here is the code which is executed, and the variables and values after execution of the code. enter image description here

Community
  • 1
  • 1
Adam S
  • 8,245
  • 17
  • 60
  • 95

5 Answers5

5

If it doesn't have to be Regex, and if it doesn't have to be slow :-) this should do it for you:

var components = "2.2 1.1-6.9,2.3-12.8 2.3".Replace("-", ",-").
Split(new[]{' ', ','},StringSplitOptions.RemoveEmptyEntries);

Components would then contain:[2.2 1.1 -6.9 2.3 -12.8 2.3]

rasmusvhansen
  • 1,147
  • 1
  • 10
  • 13
  • +1 seems like a great solution for someone that is having a hard time with regular expressions. – juharr Mar 20 '11 at 22:02
1

Does it need to be split? You could do Regex.Matches(text, @"\-?[\d]+(\.[\d]+)?").

If you need split, Regex.Split(text, @"[^\d.-]+|(?=-)") should work also.

P.S. I used Regex Hero to test on the fly http://regexhero.net

Andy Edinborough
  • 4,199
  • 1
  • 26
  • 28
1

Unless I'm missing the point entirely (it's Sunday night and I'm tired ;) ) I think you need to concentrate more on matching the things you do want and not the things you don't want.

Regex argsep = new Regex(@"\-?[0-9]+\.?[0-9]*");
string text_to_split = "-2.2 1.1-6.9,2.3-12.8 2.3 293.46701,72.238185";
var tmp3 = argsep.Matches(text_to_split);

This gives you a MatchCollection of each of the values you wanted.

To break that down and try and give you an understanding of what it's saying, split it up into parts:

\-? Matches a literal minus sign (\ denotes literal characters) zero or one time (?)
[0-9]+ Matches any character from 0 to 9, one or more times (+)
\.? Matches a literal full stop, zero or one time (?)
[0-9]* Matches any character from 0 to 9 again, but this time it's zero or more times (*)

You don't need to worry about things like \s (spaces) for this regex, as the things you're actually trying to match are the positive/negative numbers.

Town
  • 14,143
  • 3
  • 47
  • 71
  • If you look at the very first string in the OP's question, it does say `-2.2`, he must have copied it wrong the second time. – jb. Mar 20 '11 at 21:14
  • Ah yes, so it does! It definitely *is* Sunday night and I definitely *am* tired ;) Regex still works regardless though, so not all bad :) – Town Mar 20 '11 at 21:17
  • Yep yep, no worries. If I were to use regex to solve this it would look pretty darn close to yours. – jb. Mar 20 '11 at 21:18
  • It has a couple of minor problems. `4711.` is matched as a number and `.4711` is matched as `4711`. – Jonas Elfström Mar 20 '11 at 21:34
  • 1
    http://stackoverflow.com/questions/4246077/simple-problem-with-regular-expression-only-digits-and-commas/4247184#4247184 makes me think that it's probably better to split than to try to match numbers. – Jonas Elfström Mar 20 '11 at 21:40
  • Haha! Good point, well made. That reference is a pedant's delight! It could be argued that 4711. should be matched as a number (double.Parse() will parse it as 4711.0). .4711 shouldn't be matched as 4711 though, which is the case for Andy's one too. The OP gave a few examples and both regexes match the provided examples though. – Town Mar 20 '11 at 21:50
0

Consider using the string split function. String operations are way faster than regular expressions and much simpler to use/understand.

Zebi
  • 8,136
  • 1
  • 31
  • 41
0

If the "Matches" approach doesnt work you could perhaps hack something in two steps?

Regex RE = new Regex(@"(-?[\d.]+)|,|\s+");
RE.Split(" -2.2,1.1-6.9,2.3-12.8,2.3 ")
       .Where(s=>!string.IsNullOrEmpty(s))

Outputs:

-2.2
1.1
-6.9
2.3
-12.8
2.3
svrist
  • 6,657
  • 7
  • 38
  • 64