What does "?=" do in regex? - java

Can anyone tell me what "?=" does when using regex?
Here is an example of the code fragment I am trying to decipher:
password.matches("(?=.*\\d.*\\d.*)^[\\w]{8}.*$");
Thanks.

It's a positive lookahead. In that particular expression, it is saying that your password must have at least two digits (\d).
Also note that a lookahead doesn't consume input, it is merely an assertion.
For example, in your regex, the lookahead part ((?=.*\\d.*\\d.*)) asserts that your password contains at least two digits, and the rest of the expression consumes the entire string, and tries to match at least 8 word characters (i.e., [a-zA-Z_0-9]) at the beginning of the string.

It's a lookahead: A zero-width match that checks to see if the position is followed by the given expression.
http://www.regular-expressions.info/lookaround.html
In your scenario, you are looking for a string that:
begins with a string containing two digits (enforced by the lookahead)
begins with 8 word characters (matched by the rest of the regex)
The lookahead is not actually part of the match. It behaves much like a word boundary (\b) or beginning of string (^).

Related

Regex return true if even a substring follows the pattern

I was just practicing regex and found something intriguing
for a string
"world9 a9$ b6$" my regular expression "^(?=.*[\\d])(?=\\S+\\$).{2,}$"
will return false as there is a space in between before the look ahead finds the $ sign with at least one digit and non space character.
As a whole the string doesn't matches the pattern.
What should be the regular expression if I want to return true even if a substring follows a pattern?
as in this one a9$ and b6$ both follow the regular expression.
You can use
^(?=\D*\d)(?=.*\S\$).{2,}$
See the regex demo. As The fourth bird mentions, since \S\$ matches two chars, you may simply move the pattern to the consuming part, and use ^(?=\D*\d).*\S\$.*$, see this regex demo.
Details
^ - start of string (implicit if used in .matches())
(?=\D*\d) - a positive lookahead that requires zero or more non-digit chars followed with a digit char immediately to the right of the current location
(?=.*\S\$) - a positive lookahead that requires zero or more chars other than line break chars, as many as possible, followed with a non-whitespace char and a $ char immediately to the right of the current location
.{2,} - any two or more chars other than line break chars, as many as possible
$ - end of string (implicit if used in .matches())
Mostly, knock out the ^ and $ bits, as those force this into a full string match, and you want substring matches. In general, look-ahead seems like a mistake here, what are you trying to accomplish by using that? (Look-ahead/look-behind is rarely needed in general). All you need is:
Pattern.compile("\\S+\\$");
possibly, if you want an element (such as a9$) to stand on its own, use \b which is regexpese for word break: Basically, whitespace (and a few other characters, such as underscores. Most non-letter, non-digits characters are considered a break. Think [^a-zA-Z0-9]) - but \b also matches start/end of input. Thus:
Pattern.compile("\\b\\S+\\$\\b")
still matches foo a9$ bar, or a9$ just fine.
If you MUST put this in terms of a full match, e.g. because matches() (which always does a full string match) is run and you can't change that, well, put ^.* in front and .*$ at the back of it, simple as that.
Absolutely nothing about this says "This can only be needed with lookahead".

Constructing a specific regex

I want to make a regular expression in Java, with the next criteria:
Length: 10 characters exactly. Not more, not less.
Can accept any character between A-Z (only uppercase letters) and between digits 0-9.
Can accept only one dash character '-' in any position. It cannot accept any other characters, strictly only one dash.
EXAMPLES:
ABCD-12345
F-01234GHK
09-PL89GG5
LJ8U9N3-Y2
PLN86D4V-1
I have been making tries with regex of my own invention, some regular expressions that are close to the result I want, but with no success.
Do I have to combine two regular expressions?
Please, help me to get rid of this issue.... and thanks in advance!!!
I think you need lookahead (which is a way of combining two regular expressions, sort of).
^(?!.*-.*-)[A-Z0-9-]{10}$
The second part will match 10 characters that are A-Z, 0-9, or dash; the first part is negative lookahead that will reject a pattern that has two dashes in it.
You can use this:
^(?![^-]*+-[^-]*+-)[A-Z0-9-]{10}$
Note: If you use the matches method you can remove anchors.

Regular Expression negative lookahead for Vowels

I would like a Java regular expression string that finds all vowels in a string unless they:
are the first character or
the next character following an underscore
AREA_ID becomes
AR_ID
LONG_NAME becomes
LNG_NM
HOME_ALONE becomes
HM_ALN
I have played around with http://gskinner.com/RegExr and
I currently have the following regex that replaces all vowels except if it is the starting character
(?!^[AEIOU])[AEIOU]
I can't figure out how to get the second part (ignore vowel immediately following an underscore).
I'm guessing you're using JavaScript, in which case this will do:
(?!(?:^|_))_?[AEIOU]
However, if you're using a regex flavour that supports lookbehinds, try this:
(?<!^)(?<!_)[AEIOU]
Note that two lookbehinds are needed because a lookbehind must have a fixed length, which "either the start of the string or an underscore" does not.

Java Regular Expression for number of exactly 5 digits anywhere in the string

I'm trying to create a regular expression to parse a 5 digit number out of a string no matter where it is but I can't seem to figure out how to get the beginning and end cases.
I've used the pattern as follows \\d{5} but this will grab a subset of a larger number...however when I try to do something like \\D\\d{5}\\D it doesn't work for the end cases. I would appreciate any help here! Thanks!
For a few examples (55555 is what should be extracted):
At the beginning of the string
"55555blahblahblah123456677788"
In the middle of the string
"2345blahblah:55555blahblah"
At the end of the string
"1234567890blahblahblah55555"
Since you are using a language that supports them use negative lookarounds:
"(?<!\\d)\\d{5}(?!\\d)"
These will assert that your \\d{5} is neither preceded nor followed by a digit. Whether that is due to the edge of the string or a non-digit character does not matter.
Note that these assertions themselves are zero-width matches. So those characters will not actually be included in the match. That is why they are called lookbehind and lookahead. They just check what is there, without actually making it part of the match. This is another disadvantage of using \\D, which would include the non-digit character in your match (or require you to use capturing groups).

Java regex mix two patterns

How can i get this pattern to work:
Pattern pattern = Pattern.compile("[\\p{P}\\p{Z}]");
Basically, this will split my String[] sentence by any kind of punctuation character (p{P} or any kind of whitespace (p{Z}). But i want to exclude the following case:
(?<![A-Za-z-])[A-Za-z]+(?:-[A-Za-z]+){1,}(?![A-Za-z-])
pattern explained here: Java regex patterns
which are the hyphened words like this: "aaa-bb", "aaa-bb-cc", "aaa-bb-c-dd". SO, i can i do that?
Unfortunately it seems like you can't merge both expressions, at least as far as I know.
However, maybe you can reformulate your problem.
If, for example, you want to split between words (which can contain hyphens), try this expression:
(?>[^\p{L}-]+|-[^\p{L}]+|^-|-$)
This should match any sequence of non-letter characters that are not a minus or any minus that is followed my a non-letter character or that is the first or last character in the input.
Using this expression for a split should result in this:
input="aaa-bb, aaa-bb-cc, aaa-bb-c-dd,no--match,--foo"
ouput={"aaa-bb","aaa-bb-cc","aaa-bb-c-dd","no","match","","foo"}
The regex might need some additional optimization but it is a start.
Edit: This expression should get rid of the empty string in the split:
(?>[^\p{L}-][^\p{L}]*|-[^\p{L}]+|^-|-$)
The first part would now read as "any non-character which is not a minus followed by any number of non-character characters" and should match .-- as well.
Edit: in case you want to match words that could potentially contain hyphens, try this expression:
(?>(?<=[^-\p{L}])|^)\p{L}+(?:-\p{L}+)*(?>(?=[^-\p{L}])|$)
This means "any sequence of letters (\p{L}+) followed by any number of sequences consisting of one minus and at least one more letters ((?:-\p{L}+)*+). That sequence must be preceeded by either the start or anything not a letter or minus ((?>(?<=[^-\p{L}])|^)) and be followed by anything that is not a letter or minus or the end of the input ((?>(?=[^-\p{L}])|$))".

Categories