Simple java regexp problem - java

Why this code would fail?
assertTrue(Pattern.matches("[^a-zA-Z0-9]", "abc;"));

Because the .matches() method tries to match the entire string, and your regex doesn't match the entire string, only the semicolon. The Matcher.find() method would work (in this case: find a character that is not a letter between a and z nor a number between 0 and 9. Of course, it will also find á, ö etc.)
What is it you really want to do?

If fails because matches tries to match the complete string, your regexp matches 1 character which is not in the character ranges you list, if you change to:
assertTrue(Pattern.compile("[^a-zA-Z0-9]").matcher("abc;").find());
it should assert true.

Because Pattern.matches() is equivalent to the corresponding pattern being compiled and fed to Matcher.matches() which, as specified, checks to see if the entire input matches the pattern. If you only want to match part of the input, you'll want to use Matcher.find() instead.

try "^[a-zA-Z0-9]" as pattern

Because when you put the ^ inside, it means any char not in a-z or A-Z. What you want is ^[a-zA-Z0-9]

Related

Exclude a letter in Regex Pattern

I am trying to create a Regex pattern for <String>-<String>. This is my current pattern:
(\w+\-\w+).
The first String is not allowed to be "W". However, it can still contain "W"s if it's more than one letter long.
For example:
W-80 -> invalid
W42-80 -> valid
How can this be achieved?
So your first string can be either: one character but not W or 2+ characters. Simple pattern to achieve that is:
([^W]|\w{2,})-\w+
But this pattern is not entirely correct, because now it allows any character for first part, but originally only \w characters were expected to be allowed. So correct pattern is:
([\w&&[^W]]|\w{2,})-\w+
Pattern [\w&&[^W]] means any character from \w character class except W character.
Just restrict the last char to "any word char except 'W'".
There are a couple of ways to do this:
Negative look-behind (easy to read):
^\w+(?<!W)-\w+$
See live demo.
Negated intersection (trainwreck to read):
^\w*[\w&&[^W]]-\w+$
See live demo.
——
The question has shifted. Here’s a new take:
^.+(?<!^W)-\w+
This allows anything as the first term except just "W".

Word that matches ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$

I am totally confused right now.
What is a word that matches: ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
I tried at Regex 101 this 1Test#!. However that does not work.
I really appreciate your input!
What happens is that your regex seems to be in Java-flavor (Note the \\d)
that is why you have to convert it to work with regex101 which does not work with jave (only works with php, phyton, javascript)
see converted regex:
^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
which will match your string 1Test#!. Demo here: http://regex101.com/r/gE3iQ9
You just want something that matches that regex?
Here:
a1a!
This pattern matches
\dTest#!
if u want a pattern which matches 1Test#! try this pattern
^.(?=.\d)(?=.[a-zA-Z])(?=.[!##$%^&]).*$
Your java string ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$ encodes the regexp expression ^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$.
This is because the \ is an escape sequence.
The latter matches the string you specified.
If your original string was a regexp, rather than a java string, it would match strings such as \dTest#!
Also you should consider removing the first .*, doing so would make the regexp more efficient. The reason is that regexp's by default are greedy. So it will start by matching the whole string to the initial .*, the lookahead will then fail. The regexp will backtrack, matchine the first .* to all but the last character, and will fail all but one of the loohaheads. This will proceed until it hits a point where the different lookaheads succeed. Dropping the first .*, putting the lookahead immidiately after the start of string anchor, will avoid this problem, and in this case the set of strings matched will be the same.

Formulating a regex with a single dot

I am trying to formulate a regex for the following scenario :
The String to match : mName87.com
So, the string may consist of any number of alpha numeric characters , but can contain only a single dot anywhere in the string .
I formulated this regex : [a-zA-Z0-9.], but it matches even multiple dots(.)
What am i doing wrong here ?
The regex you provided matches only a single character in the whole string you're trying to validate. There are a few things to take care of in your scenario
You want to match over the whole string, so your regex must start with ^ (beginning of the string) and end with $ (end of the string).
Then you want to accept any number of alpha-numeric characters, this is done with [a-zA-Z0-9]+, here the + means one or more characters.
Then match the point: \. (you must escape it here)
Finally accept more characters again.
All together the regex would then be:
^[a-zA-Z0-9]+\.[a-zA-Z0-9]+$
You can use this regex:
\\w*\\.\\w*
You can try here
Try with:
^([a-zA-Z0-9]+\.)+[a-zA-Z]$
use this regular expression ^[a-zA-Z0-9]*\.[a-zA-Z0-9.]*$
EDITED:
Try
([a-zA-Z0-9]+\.[a-zA-Z0-9]+)|(\.[a-zA-Z0-9]+)|([a-zA-Z0-9]+\.)
That is: [a word that ends with a dot] OR [two words and the dot in the middle] OR [a word that starts with a dot]

Regular Expression allowing special characters

I am using the following pattern to match my String:
[a-zA-Z0-9]*
Even when I am passing the String *$#, its getting matched by the regular expression. Could someone explain what am I doing wrong or why this is happening?
You should use ^(start of the string) and $(end of the string).
So,the regex would be
^[a-zA-Z0-9]*$
[a-zA-Z0-9]* would match anywhere in the string if you use find method..Using ^ and $ would match the entire input from start till end
if you use matches method you don't need to have ^,$ as it tries to match the entire string
[a-zA-Z0-9]* means 0 or more of any of those characters. If you're using Matcher.find(), it'll find that anywhere/everywhere because it can match anywhere in a string.

Regular expression ([A-Za-z]*) matches digits and special characters?

i need to validate a text fiel in my application. this cant contain neither digit nor special char so i tried this regex:
[A-Za-z]*
The problem is that this regex doesn't work when i put a digit or a special char in the middle or at the end of the String.
You should use it like this:
^[A-Za-z]+$
to match text (1 or more in length) containing ASCII letters only.
Go ahead and try ^[A-Za-z]*$ instead.
You could use the following Regex:
Pattern p = Pattern.compile("^[A-Za-z]*$");
See a list of regex-specs on http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
The pattern you describe will never work. Without the begin and end bonds the pattern will look for a substring that matches. Since an empty string is also allowed (star means 0 or more characters), one can simply use the empty string anywhere.
you are check help me validate date time type YYYY-MM-DDThh:mm:ss.sssZ or
YYYY-MM-DD
^(((19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]))|(((19|20)[0-9][0-9])-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])T([01]?[0-9])|([2][0123]):([012345]?[0-9]):([012345]?[0-9])\.([0-9][0-9][0-9][Z]))$

Categories