1

What does (?=.*[^a-zA-Z]) mean

I am a beginner in regex and not getting what does it mean .

Is it like, dot(.) means any character so .* means any character any number of times and [^a-zA-z] any one character except a-z and A-Z. what string will match it?

Thanks,

Puneet

Puneet Garg
  • 45
  • 2
  • 7
  • 1
    When you don't understand a regex such as this go to one of the online tools like http://gskinner.com/RegExr/ , paste in your reg ex, then hover over each part of the reg ex for a tool-tip explanation of what it does. – Paul R Jul 27 '12 at 08:58
  • See also: http://stackoverflow.com/questions/2491930/is-there-an-online-regexbuddy-like-regular-expression-analyzer – Paul R Jul 27 '12 at 08:59
  • I swear by having a play around with [The Regex Coach](http://www.weitz.de/regex-coach/) for working this sort of thing out. There's a bunch of alternatives, too, including some that are web based. Regex Coach (amongst other things) gives a description of the regex; here saying it's a "zero-width positive look-ahead assertion", which you can then use to find out what that means on [Regular-Expressions.info](http://www.regular-expressions.info/). – me_and Jul 27 '12 at 09:01

1 Answers1

7

That is positive look ahead assertion.

That means that there are at least one symbol that is not a-ZA-Z to right from the point.

Example:

$ echo 12abc | grep -P '2(?=.*[^a-zA-Z])'
$ echo 12abc. | grep -P '2(?=.*[^a-zA-Z])'
12abc.

In the first line there are no not a-zA-Z after 2. And the line will not be shown.

In the second line I've added point to the end. Now there is a not a-zA-Z after 2. And the line will be found and shown.

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144