What is wrong in regexp?

regular expression examples
when to use regex
regex now you have two problems
python regex
javascript regex matcher
email regex
online perl regex
regex performance tester

I don't understand, why this regexp works not as I expect:

Regexp: ^<prefix>(.*?)(<optTag.*?>)?(.*?)<postfix>$

Test: <prefix>some chars<optTag value>some chars<postfix>

Test result:

Group 1: Empty

Group 2: Empty

Group 3: some chars<optTag value>some chars

I would expect that group 2 = <optTag value>

You can't use a non-greedy wildcard preceding an optional capture group. Use this instead:


What's wrong with my regex?, You could also go for the case statement: REGEX="[2][0-2]:[0-5][0-9]"; # Note no placeholders like ^ and $ here TIME="21:30" case $TIME in  Re: What's wrong with the regular expression? 807605 Jul 26, 2007 12:26 AM ( in response to 807605 ) A word boundary (in the context of regexes) is a position that is either followed by a word character and not preceded by one (start of word) or preceded by a word character and not followed by one (end of word).

Kind of a pain, but you could put a block assertion in those (.*?) groups.




 (                             # (1 start)
           (?! <optTag .*? > )
 )                             # (1 end)
 ( <optTag .*? > )?            # (2)
 (                             # (3 start)
           (?! <optTag .*? > )
 )                             # (3 end)

Regular Expressions: Now You Have Two Problems, Perl's nature encourages the use of regular expressions almost to the to decipher regular expression in your codebase, they did it wrong. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more What is wrong with this regex pattern in R?

You can add the word boundaries "\b" in your regular expression to get the required value in Group 2.

This ReGeX worked for me,


You can read more here.

Regexes: the Bad, the Better, and the Best, What, you ask, would be the difference between these two regular expressions? What is it that makes a good regular expression and a bad one  Historically, you are of course correct. The word "regex" is a contraction of the words "regular expression". But the things that we call regular expressions are completely different than the things we call regex.

Common Regular Expression Mistakes, Common Regular Expression Mistakes. Forgetting to enclose the regular expression in ' '. Including spaces in the regular expressions (other  What is the problem with this regular expression when I use the global flag and the case insensitive flag? Query is a user generated input. The result should be [true, true]. var query = 'Foo B';

Regular Expression Short-Answer question type, Incorrect Answers are Answers which have a grade equal to zero (or None). When you write those Incorrect Answers, you  To find a match, the regular expression engine uses the following algorithm: For every position in the string Try to match the pattern at that position. If there’s no match, go to the next position. These common words do not make it obvious why the regexp fails, so let’s elaborate how the search works for the pattern ".+".

SyntaxError: invalid regular expression flag "x", What went wrong? There are invalid regular expression flags in the code. In a regular expression literal, which consists of a  A regular expression is a pattern used to match text. It can be made up of literal characters, operators, and other constructs. This article demonstrates regular expression syntax in PowerShell. PowerShell has several operators and cmdlets that use regular expressions. You can read more about their syntax and usage at the links below. Select-String

  • Since group 2 and 3 are optional, group 1 or group 3 will consume it all depending on the input.
  • I don't think you will be able to use a regex to capture all possible cases unless the input space is very confined. For instance, if the first group can contain a prefix of <optTag... (without the closing angular bracket) then you will have nested patterns, and you cannot use a regex to match arbitrarily deep nesting.
  • This will fail if the 1st group contains <.
  • This won't work if you drop <optTag value> from the test string.
  • Yes, of course, it won't. The op hasn't mentioned anything about varying "optTag" part of the entire "<optTag value>" part. Maybe he is looking only for the varying "value".
  • The OP's original regex has (<optTag.*?>)? clearly indicating the whole <optTag value> part is optional.