1

I am using this regex pattern (\[|)(\w\w \d*)(\; |\*|\s|]?, )((\"(\w)\"|\((.\w)\))|)(\]|)|(\[\w\w \d\]) to match the below string:

Hp 0; Ks 1; Ks 2

I have tested this pattern in https://regex101.com/, and everything works fine, complete string is matched Hp 0; Ks 1; Ks 2 . But the same pattern in Visual Studio (.Net Framework) match just Hp 0; Ks 1;.

Can anyone help me where is the problem ?

I have written this code in VS:

 StringBuilder matchedSignalbegriff = new StringBuilder();

            var pattern = "(\\[|)(\\w\\w \\d*)(\\; |\\*|\\s|]?, )((\"(\\w)\"|\\((.\\w)\\))|)(\\]|)|(\\[\\w\\w \\d\\])";
            Regex rgx = new Regex(pattern);

            foreach (Match match in rgx.Matches(stringToTest))
            {
                matchedSignalbegriff.Append(match);
            }
7
  • You'll be a lot happier writing verbatim string literals (i.e. literals prefaced with a @) when doing Regexes. Something like var pattern = @"(\[|)(\w\w etc. In all likelihood, you have a misplaced escape character if it works in a tool and not in your code Commented Mar 5, 2022 at 23:02
  • Apart from not using a verbatim string as @Flydog57 suggests, is there a reason why your pattern is so complicated? What other input variations are you trying to match? Commented Mar 5, 2022 at 23:24
  • Even if i use your pattern in regex101.com, Hp 0; and Ks 1; are two separate matches. To match the whole string Hp 0; Ks 1; Ks 2, you could use Regex rgx = new Regex(@"(\[|)((\w\w \d*)((\; )?|\*|\s|]?, ))*((\""(\w)\""|\((.\w)\))|)(\]|)|(\[\w\w \d\])"); Commented Mar 5, 2022 at 23:27
  • @PeterE it looks complicated, because this is just one of 200 matches! This pattern match 200 different strings =) Commented Mar 6, 2022 at 9:25
  • 1
    @DevOp maybe then you have to describe what you want to achieve a bit more in detail. While using the regex I provided, the whole string you described is matched. You can check it out in this .net fiddle. Commented Mar 6, 2022 at 11:08

1 Answer 1

1

Your pattern does not match the whole string or all 3 parts on regex101, see https://regex101.com/r/2ldB6Z/1

That is because this part (\; |\*|\s|]?, ) has to match at least one of the listed character(s) which is not present in Hp 0; Ks 1; Ks 2

Using a construct like (\[|) in your pattern makes it optional due to the | at the end.

To make the pattern match all 3 parts, you can add asserting the end of the string in the alternation in your pattern:

var pattern = @"(\[|)(\w\w \d*)(\; |\*|\s|]?, |$)((""(\w)""|\((.\w)\))|)(\]|)|(\[\w\w \d\])";

See a .NET regex demo and a C# demo

As an alternative for a more basic pattern to match the example string:

\w\w \d+(?:; \w\w \d+)*

See another regex demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.