I want to match alphanumeric characters and it must contain digits compulsorily.
Basically, I want to extract an order number which is a combination of alphabets, digits and a few special characters. I wrote the following regex
String invoiceRegex = "(?<=((?i)(PO|P/O|ORDER)([\\s|.]{0,4})(number|no)?[|: -.]{0,10}))([\\dA-Z:-]*)";
But then it matches the invalid information such as IMMEDIATELY and other words. So I want a regex that matches alphanumeric characters with digits mandatory.
ex: From text "P/O No. : P9:8774" i want P9:8774.
I solved the problem.I made a group with alphabets an option and digit mandatory.and then repeated this group with +.
now it looks something like this. an
String invoiceRegex = "(?<=((?i)(PO|P/O|ORDER)([\\s|.]{0,4})(number|no)?[|: -.]{0,10}))([A-Z:-]*\\d+)+";
Related
We have password requirements:
Must contain capital letters
Must contain lowercase letters
Must contain numbers
Must contain special characters
There should be no characters repeating one after another
Now our validation regex is:
^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%&*]))
So it doesn't validating the 5th requirement.
How to improve regex to validate characters repeating?
You can remove the outer capture group, and then use a negative lookahead with a backreference to group 1 to exclude 2 repeating characters on after another.
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%&*])(?!.*(.)\1)
In Java
String regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%&*])(?!.*(.)\\1)";
Regex demo
Note that if using the pattern only for a password validation, the minimum length is just 4 characters.
To reduce some backtracking, you could also use negation:
^(?=[^\r\n\d]*\d)(?=[^\r\na-z]*[a-z])(?=[^\r\nA-Z]*[A-Z])(?=[^\r\n!##$%&*]*[!##$%&*])(?!.*(.)\1)
Regex demo
I am looking for a regex that matches only when it sees a string that is randomly filled by digits and chars.
For example, adfak332arg3 is allowed but 332352 and fagaaah are not allowed. .*[^\\s] looks fine for strings with only chars but how to fix it to accepts the desired strings and refuses the other two types?
Use a positive lookahead (?=) to ensure that the string contains required characters.
^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]+$
Test this regex pattern here.
You can try this regex
"[\\d\\w]*\\d\\w[\\d\\w]*|[\\d\\w]*\\w\\d[\\d\\w]*"
If you need just a mixed string of characters A-Z, a-z and 0-9 you can use:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])$
If you want to force the string to have a minimum number of characters in your string you can use (e.g. minimum 8 in the string):
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$
If you want to have a string length from min-length to max-length then use (e.g. string of at least 5 characters and max 20 characters):
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{5,20}$
To ensure that an input contains digits as well as characters, you could use this regex:
^(?:[A-Za-z]+\\d+|\\d+[A-Za-z]+)[A-Za-z\\d]*$
The regex ensures that the input contains at least a number and a character, and allows only numbers or characters (no special characters etc.)
(?:[A-Za-z]+\d+|\d+[A-Za-z]+) ensures that it starts with one or more characters followed by digits or alternatively |\d+[A-Za-z]+ one or more digits followed by one or more characters
[A-Za-z\d]* allows any number of characters or digits after the previous check
^ and $ to match starting and ending anchor
Regex101 Demo
Hope this helps!
Try this Regex.
[A-z][0-9]|[0-9][A-z]
I have written a regex to omit the characters after the first occurrence of some characters (, and #)
String number = "(123) (456) (7890)#123";
number = number.replaceAll("[,#](.*)", ""); //This is the 1st regex
Then a second regex to get only numbers (remove spaces and other non numeric characters)
number = number.replaceAll("[^0-9]+", ""); //This is the 2nd regex
Output: 1234567890
How can I merge the two regex into one like piping the O/p from first regex to the second.
You can combine both regex in the following way.
String number = "(123) (456) (7890)#123";
number = number.replaceAll("[,#](.*)", "").replaceAll("[^0-9]+", "");
So you need to remove all symbols other than digits and the whole rest of the string after the first hash symbol or a comma.
You cannot just concatenate the patterns with |operator because one of the patterns is anchored implicitly at the end of the string.
You need to remove any symbols but digits AND hashes with commas first since the tegex engine processes the string from left to right and then you can add the alternative to match a comma or hash with any text after them. Use DOTALL modifier in case you have newline symbols in your input.
Use
(?s)[,#].*$|[^#,0-9]+
I want to make a regular expression to the following string :
String s = "fdrt45B45"; // s is a password (just an example) with only
// letters and digits(must have letters and digits)
I tried with this pattern:
String pattern= "^(\\w{1,}|\\d{1,})$"; //but it doesn't work
I want to get a not match if my password doesn't contains letters or digits and a match if its contains both.
I know that: \w is a word character: [a-zA-Z_0-9] and \d is a digit: [0-9], but i can not mix \w and \d to get what i want. Any help or tips is very appreciated for a newbie.
A positive lookahead would do the trick :
String s = "fdrt45B45";
System.out.println(s.matches("(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9]+"));
You should be able to use the following regex to achieve what you want. This uses a positive look ahead and will match any string containing at least one letter and at least one number.
^(?=.*\\d)(?=.*\\w).*
How can I match all numbers along with specific characters in a String using regex? I have this so far
if (!s.matches("[0-9]+")) return false;
I don't understand much regex, but this matches all characters from 0-9 and now I need to be able to match other specific characters, for example "/", ":", "$"
You can use this regex by including those symbols in a character class:
s.matches("[0-9$/:]+")
Read more about character class
You can add the other characters that you need to match to the end of the character group, like this:
if (!s.matches("[0-9/:$]+")) return false;
You need to be careful about several things:
If ^ is among the characters, it must not be the first one of the group
If - is among the characters, it must be the last one in the group
If ] is among the characters, it needs to be escaped for regex and for Java, e.g. [\\]]
If \ is among the characters, it needs to be escaped for regex and for Java, e.g. [\\\\]
Regex:
String regex = "\\d/:$+";