Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Using lookahead assertion to check if character exist at most a certain number of times

Tags:

python

regex

How do I use lookahead assertion to determine if a certain character exist at most a certain number of times in a string.

For example, let's say I want to check a string that has at least one character to make sure that it contains "@" at most 2 times. Thanks in advance. Using python if that matters.

like image 206
teggy Avatar asked Mar 05 '10 00:03

teggy


People also ask

Can I use regex lookahead?

Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.

What does lookahead do regex?

Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions.

Can I use regex Lookbehind?

The good news is that you can use lookbehind anywhere in the regex, not only at the start.

What is lookahead and Lookbehind?

The lookbehind asserts that what immediately precedes the current position is a lowercase letter. And the lookahead asserts that what immediately follows the current position is an uppercase letter.


1 Answers

There are lots of ways to do this, for example:

/^(?=([^@]*@){,2}[^@]*$)./
like image 58
Mark Byers Avatar answered Nov 11 '22 00:11

Mark Byers