I am trying to use regular expressions to parse a method in the following format from a text:
mvAddSell[value, type1, reference(Moving, 60)]
so using the regular expressions, I am doing the following
tokensizedStrs = Regex.Split(target, "([A-Za-z ]+[\\[ ][A-Za-z0-9 ]+[ ,][A-Za-z0-9 ]+[ ,][A-Za-z0-9 ]+[\\( ][A-Za-z0-9 ]+[, ].+[\\) ][\\] ])");
It is working, but the problem is that it always gives me an empty array at the beginning if the string started with a method in the given format and the same happens if it comes at the end. Also if two methods appeared in the string, it catches only the first one! why is that ?
I think what is causing the parser not to catch two methods is the existance of “.+” in my patern, what I wanted to do is that I want to tell it that there will be a number of a date in that location, so I tell it that there will be a sequence of any chars, is that wrong ?
it woooorked with ,e =D … I replaced “.+” by “.+?” which meant as few as possible of any number of chars 😉
Your goal is quite unclear to me. What do you want as result? If you split on that method pattern, you will get the part before your pattern and the part after your pattern in an array, but not the method itself.
Answer to your question
To answer your concrete question: your
.+is greedy, that means it will match anything till the last)](in the same line,.does not match newline characters by default).You can change this behaviour by adding a
?after the quantifier to make it lazy, then it matches only till the first)].Problems in your regex
There are several other problems in your regex.
I think you misunderstood character classes, when you write e.g.
[\\[ ]. this construct will match either a[or a space. If you want to allow optional space after the[(would be logical to me), do it this way:\\[\\s*Use a verbatim string (with a leading
@) to define your regex to avoid excessive escaping.You can simplify your regex, by avoiding repeating parts
This is an non capturing group
(?:\s*,\s*[A-Za-z0-9 ]+){2}repeated two times.