I have a problem fine-tuning the regex
I have a regex that was ok, but since it crashed, it doesn't work in some situations.
Make sure the message editor that invokes messages does some tricky things with "\"
[\ [] [\ ^% # \ $ \ * @ \ - ;?] * [\ ^% # \ $ \ * @ \ -;.?] [\]]
his job is to find a template that looks like this in general
[ABA]
- A - char from the set ^,%, #, $, *, @, - ,;
- B - text
- [and] are included in the template
will find all occurrences of this pattern in the test string
Black fox [# sample1 #] [% sample2%] - [# sample3 #] eats blocks.
but instead of the expected list of hits
- "[# sample1 #]"
- "[% sample2%]"
- "[# sample3 #]"
I get this
- "[# sample1 #]"
- "[% sample2%]"
- "- [# sample3 #]"
And it looks like this problem will also occur with other characters in the "A" set. So can anyone suggest changes to my regex to make it work as I need it?
and less importantly, how to get my regex to exclude patterns that look like
[ABC]
- A - char from the set ^,%, #, $, *, @, - ,;
- B - text
- C - char from the set ^,%, #, $, *, @, - ,; except A
- [and] are included in the template
eg
[$ sample1 #] [% sample2 @] [% sample3;]
early
MTX
source to share
\ [([% # $ * @; ^ -]). +? \ 1 \]
applies to text:
Black fox [# sample1 #] [% sample2%] - [# sample3 #] [% sample4;] eats blocks.
coincidences
-
[#sample1#]
-
[%sample2%]
-
[#sample3#]
- but not
[%sample4;]
EDIT
This works for me (output as expected the regex accepted by C # as expected):
Regex re = new Regex(@"\[([%#$*@;^-]).+?\1\]");
string s = "Black fox [#sample1#] [%sample2%] - [#sample3#] [%sample4;] eats blocks.";
MatchCollection mc = re.Matches(s);
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
source to share
You have an optional opening square bracket match:
[\]]
For the second part of the question (and perhaps simplification), try this:
\ [\% [^ \%] + \% \] | \ [\ # [^ \ #] + \ # \] | \ [\ $ [^ \ $] + \ $ \] p>
In this case, there is an additional template for each possible delimiter. | an OR character, so it will match if one of the three subexpressions matches.
Each subexpression will be:
- Opening parenthesis
- Special Char
- Anything that is not a special char (1)
- Special Char
- Closing backet
(1) it may be necessary to add additional exceptions such as ']' or '[', so it is not accidentally matched against large text, for example:
[% MyVar #] blah blah [$ OtherVar%]
Rob
source to share