0

This is probably a really basic question, but I can't find any answers. I need to match a string by either two or more spaces OR an equals sign.

When I split this string: 9 x 13 = (8.9 x 13.4) (89 x 134) with ( +) I get:

part 0: 9 x 13 = (8.9 x 13.4)
part 1: (89 x 134)

When I split it with (=) I get:

part 0: 9 x 13 
part 1:  (8.9 x 13.4)          (89 x 134)

How can split by BOTH? Something like: (=)OR( +)

Edit: This does not work(=)|( +), I was expecting:

part 0: 9 x 13 
part 1: (8.9 x 13.4)
part 2: (89 x 134)
David
  • 14,313
  • 26
  • 100
  • 149
  • 3
    You CANT parse nested constructs with Regex alone! – leppie Jun 04 '12 at 10:32
  • Did you try: `(=)|( +)`? – Vikas Jun 04 '12 at 10:33
  • @leppie, you sure *can*, altho that doesn't mean that it's the best way to do it. – Qtax Jun 04 '12 at 10:37
  • @Qtax: sure you **can**, but the results will be not correct at all... – leppie Jun 04 '12 at 10:38
  • can you type the answer you need ? – Ahmed Ghoneim Jun 04 '12 at 10:38
  • @leppie, I don't know what you mean when you say "can", but when I say "can" here I mean that it can be done correctly giving the correct result. – Qtax Jun 04 '12 at 10:40
  • 3
    @Qtax: Parse `((()()())()())` – leppie Jun 04 '12 at 10:41
  • @leppie, ask it in a question and I, and many others, will. – Qtax Jun 04 '12 at 10:42
  • @Qtax http://www.cs.rochester.edu/~nelson/courses/csc_173/grammars/cfg.html – L.B Jun 04 '12 at 10:51
  • @leppie, `^(?:(\()(?'-1'\))*)+(?(1)(?!))$` and [example](http://ideone.com/42wNe), see [balancing groups](http://msdn.microsoft.com/en-us/library/bs2twtah.aspx#balancing_group_definition). *"Someone is wrong on the Internet!"* – Qtax Jun 04 '12 at 10:55
  • 1
    @Qtax a CFG (for ex Arithmetic Expressions) can not be parsed with regex. – L.B Jun 04 '12 at 10:58
  • @L.B, you one of those people that think that "regex" (.NET regex in this context) are regular? [Think again](http://stackoverflow.com/questions/7434272/match-an-bn-cn-e-g-aaabbbccc-using-regular-expressions-pcre). – Qtax Jun 04 '12 at 11:02
  • @Qtax: There is suppose to be 7 captured groups, not just 2 ... DOH! – leppie Jun 04 '12 at 11:03
  • @leppie, supposed? Yeah, your one word specs were very detailed. If you want another solution ask in a question. If you follow the links and check the docs you'll see that regex can do more than you think. – Qtax Jun 04 '12 at 11:12
  • @David Did you test my answer ? – Ahmed Ghoneim Jun 04 '12 at 11:34
  • @Qtax: How hard can it be? 7 groupings of `( )`, just count them. I know RegEx can do a lot, but if you use 'fancy' features like backtracking etc, your matching speed will be considerably crippled. And then you are stuck with a really hard pattern that is probably not extensible by human means. What is wrong with just using a lexical analyzer and be done with it? – leppie Jun 04 '12 at 11:49

4 Answers4

2

Your regex should have worked, except it would leave the spaces that were before and after the =. That's assuming you really did use two spaces in the ( +) part (which got normalized to one space by SO's formatting). This one yields the exact result you said you want:

@" {2,}|\s*=\s*"
Alan Moore
  • 68,531
  • 11
  • 88
  • 149
  • Why would there be two spaces? The question specifies: "one or more spaces". But maybe the question was wrong. – PauliL Jun 04 '12 at 11:06
  • @PauliL My mistake. It was meant to be two or more spaces. – David Jun 04 '12 at 11:07
  • @PauliL: I almost always initiate an edit on a questions before I answer it, to make sure I'm seeing what I'm supposed to see. In this case it revealed that there were two spaces in the first regex, and multiple spaces in the relevant portion of the source string. I tried to reformat the question so everyone could see that, but the OP overrode me. – Alan Moore Jun 04 '12 at 11:17
1

Simply,

Pattern = "\s*=\s*|(?!\))\s+?(?=\()"
Ahmed Ghoneim
  • 6,268
  • 8
  • 46
  • 76
0
(=)|( +)

Is that good for you?

Explanation and example: http://msdn.microsoft.com/en-us/library/ze12yx1d.aspx , scroll down to the 3rd remark...

TDaver
  • 7,024
  • 5
  • 44
  • 91
0

You can use a regex like this: [= ]+

var regex = new Regex("[= ]+");
var parts = regex.Split("this is=a test");

// parts = [ "this", "is", "a", "test" ]

If you want to keep the separators enclose the regex in parens: ([= ]+)

kongo2002
  • 980
  • 4
  • 10