I need to write a regular expression for a string of two parts which is separated by '.' Here below are the condition,
<<1st part>>.<<2nd part>> : Example- Time01.Sheet
1st part should contain alpha numeric characters and it must contain at least 1 uppercase alphabet, 1 lowercase alphabet, and 1 number.
2nd part should contain alpha numeric characters.
My code : ((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=\\S+$).*)[.]([\\w]+)$
Input : Vijay.hello876IUY
Actual Output : Valid data
Expected Output : Invalid data (Because 1st part doesn’t contain any numbers)
Any one please help me to solve this...
You may use
^(?=[^.]*[a-z])(?=[^.]*[A-Z])(?=[^.]*[0-9])[a-zA-Z0-9]+\.[a-zA-Z0-9]+$
See the regex demo.
Details
^ - start of string
(?=[^.]*[a-z]) - there must be a lowercase ASCII letter after 0+ chars other than .
(?=[^.]*[A-Z]) - there must be an uppercase ASCII letter after 0+ chars other than .
(?=[^.]*[0-9]) - there must be a digit after 0+ chars other than .
[a-zA-Z0-9]+ - 1+ alphanumeric chars
\. - a dot
[a-zA-Z0-9]+ - 1+ alphanumeric chars
$ - end of string.
In Java:
s.matches("(?=[^.]*[a-z])(?=[^.]*[A-Z])(?=[^.]*[0-9])[a-zA-Z0-9]+\\.[a-zA-Z0-9]+")
Since matches() requires a full string match, you need no ^ at the beginning and $ anchor at the end.
Related
Requirement is : Field is made of 1 alpha character followed by 15 alpha numeric characters including maximum 2 hyphens (-)
Regex:^(?!^(.)\\1*$)([A-Z]{1}(?=[A-Z0-9-]{2,15}$)[A-Z0-9]*(?:\\-[A-Z0-9]*){0,2}[A-Z0-9]+$)
Above Regex is working fine but as the requirement it's not allowed the duplicate numbers like 1) N000000000000000 2)N000000-0000-0
the above 2 values not to allow as its has the same digit as "0".
You can use
^[A-Z](?!\D*(\d)(?:\D*\1)+\D*$)(?=.{2,15}$)[A-Z0-9]*(?:-[A-Z0-9]*){0,2}[A-Z0-9]+$
See the regex demo. Inside the code, in your string literals, make sure you escape backslashes twice.
Details:
^ - start of string
[A-Z] - an uppercase ASCII letter
(?!\D*(\d)(?:\D*\1)+\D*$) - the string cannot contain only identical digits
(?=.{2,15}$) - there must be 2 to 15 chars other than line break chars to the right of the current position
[A-Z0-9]* - zero or more ASCII uppercase letters/digits
(?:-[A-Z0-9]*){0,2} - zero, one or two occurrences of - and then zero or more ASCII uppercas eletters/digits
[A-Z0-9]+ - one or more ASCII uppercase letters/digits
$ - end of string.
`
I want to check if a string consists of letters and digits only, and allow a - separator:
^[\w\d-]*$
Valid: TEST-TEST123
Now I want to check that the separator occurs only once at a time. Thus the following examples should be invalid:
Invalid: TEST--TEST, TEST------TEST, TEST-TEST--TEST.
Question: how can I restrict the repeated occurrence of the a character?
You may use
^(?:[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)?$
Or, in Java, you may use an alphanumeric \p{Alnum} character class to denote letters and digits:
^(?:\p{Alnum}+(?:-\p{Alnum}+)*)?$
See the regex demo
Details
^ - start of the string
(?: - start of an optional non-capturing group (it will ensure the pattern matches an empty string, if you do not need it, remove this group!)
\p{Alnum}+ - 1 or more letters or digits
(?:-\p{Alnum}+)* - zero or more repetitions of
- - a hyphen
\p{Alnum}+ - 1 or more letters or digits
)? - end of the optional non-capturing group
$ - end of string.
In code, you do not need the ^ and $ anchors if you use the pattern in the matches method since it anchors the match by default:
Boolean valid = s.matches("(?:\\p{Alnum}+(?:-\\p{Alnum}+)*)?");
I need a regex to match a text with special chars -,.+\/& in it. The special chars must not be more than 2 subsequent and a special char can not be followed by space. More specifically I have to cover these cases:
some text/
/some text
some /text
I came up with this regex:
^[-\/,\.+\&]{0,1}[\p{L}]+[-\/,\.+\&]{0,1}([\s\-']?[-\/,\.+\&]{0,1}[\p{L}]+)([-\/,\.+\&]{0,1})$
It matches most of the cases that I need but fails to match for instance:
some te&xt. Every help will be appreciated. Thanks.
You can use
"^(?!.*(?:[-,.+/&]\\s|[-,.+/&]{2}))[^\\s\\d]+(?:\\s+[^\\s\\d]+)*$"
See the regex demo
Explanation:
^ - start of string
(?!.*(?:[-,.+/&]\\s|[-,.+/&]{2})) - a negative lookahead that fails the match if there is a special char [-,.+/&] followed with a whitespace \s, or 2 consecutive special chars from [-,.+/&] set
[^\\s\\d]+ - 1 or more characters other than digit and whitespace
(?:\\s+[^\\s\\d]+)* - 0+ sequences of:
\\s+ - 1+ whitespaces
[^\\s\\d]+ - 1 or more characters other than digit and whitespace
$ - end of string
I found the solution:
^[-\/,\.+\&\s]{0,1}([\p{L}][-\/,\.+\&\s]{0,1})+([-\/,\.+\&\s]{0,1}([\p{L}][-\/,\.+\&\s]{0,1})+)([\p{L}][-\/,\.+\&\s]{0,1})([-\/,\.+\&\s]{0,1})$
I have following regex which doesn’t match two different strings.
Actual regex which finds AB-434. Which doesn’t match TEMS-54534.
([a-zA-Z][a-zA-Z0-9_]+-[1-9][0-9]*)([^.]|\.[^0-9]|\.$|$)
here is the sample inputs
TEMS-54534
TEMS-5453
TEMS-1233
TEMS-12
CB-213
CB-2135
CB-12
ABC-2223
ABC-223
ABC-12
You seem to be looking for a pattern that starts with 1 ASCII letter followed with 1 or more alphanumeric or underscore characters followed with a - followed with one or more digits not starting with 0.
You can use
^[a-zA-Z][a-zA-Z0-9_]+-[1-9][0-9]*$
or
^[a-zA-Z]\w+-(?!0)\d+$
See the regex demo (and another one).
Explanation:
^ - start of string
[a-zA-Z][a-zA-Z0-9_]+ / [a-zA-Z]\w+ - an ASCII letter followed with 1+ alphanumerics/underscore chars
- - a hyphen
[1-9][0-9]* / (?!0)\d+ - a digit from 1-9 range followed with 0+ nay digits (you can restrict it with {min,max} limiting quantifier if need be)
$ - end of string
More details:
[a-zA-Z0-9_] can be written as \w (if no Pattern.UNICODE_CHARACTER_CLASS is used)
In Java, do not forget to use double backslashes to escape metacharacters and shorthand character classes
If the pattern is used with String#matches(), the ^ at the start and $ at the end of the pattern are redundant.
And a Java demo:
List<String> strs = Arrays.asList("TEMS-54534","TEMS-5453","TEMS-1233","TEMS-12","CB-213",
"CB-2135","CB-12","ABC-2223","ABC-223","ABC-12");
for (String str : strs)
System.out.println(str.matches("[a-zA-Z]\\w+-(?!0)\\d+"));
All:
What I want to do is using Regex to match a string which only allow [A-Za-z0-9_-] and the format should be:
Started with only [A-Za-z0-9], and followed by [A-Za-z0-9_-]. There could be [_-] in the middle, but if there is any, it is only allowed once(both _ and - can exist, but each one only has one chance), and ended with [A-Za-z0-9].
I only know how to match Alphanumeric characters, a dash and an underscore, but have no idea how to limit their occurrence time.
Thanks
You can use negative lookahead:
^(?!.*(-[^-]*-|_[^_]*_))[A-Za-z0-9][\w-]*[A-Za-z0-9]$
RegEx Demo
Explanation:
^ - Line start
(?!.*(-[^-]*-|_[^_]*_)) - Negative lookahead which means fail the match if there are 2 underscore or 2 hyphens ahead
[A-Za-z0-9] - Match 1 alphanumeric character
[\w-]* - Match 0 or more of [A-Za-z0-9_-] characters
$ - Match line end