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]
Related
I generate a 6 characters long random number. It can be all numeric, alphabets and alphanumeric. I have to validate this string on basis of provided regular expression. For example:
If string is numeric [0-9], then it should not contain all zeroes.
If string is alphabetic [a-zA-Z], then last character cannot be X or x. And string cannot start with SVC or svc.
If string is alphanumeric [0-9a-zA-Z], then it cannot contain all zeroes 0. And string cannot start with tripple zeroes 000 and cannot end with x or X.
I need regular expressions for these that can be used with Java Matcher.
This should work:
/^((?!.{7,})[0-9]*[1-9]+[0-9]*|(?!(SVC|svc))[a-zA-Z]{5}[a-wy-zA-WY-Z]|(?!(000|.{7,}))[0-9a-zA-Z]*([a-zA-Z][0-9]|[0-9][a-zA-Z])+[0-9a-zA-Z]*[a-wy-zA-WY-Z])$/gm
I cannot explain this regex, as it is too long and just a repetitive application of the same concepts over and over. However, given the following input, it matches only the first five lines:
002000
jfkasd
002dfd
sVcabc
abc65i
000000
00012c
0123ax
SVCabx
svcabc
abc65x
abc65X
Here's the original attempt I proposed, which does not satisfy all the condition of the OP, but it is accompained by an explanation:
/^((?!.{7,})[0-9]*[1-9]+[0-9]*|[a-zA-Z]{5}[a-wy-zA-WY-Z]|(?!000)[0-9a-zA-Z]{6})$/gm
Explanation (which could read on the linked page itself):
We have three alternatives that have to match the whole line: ^(…|…|…)$;
The 2nd alernative is easy: five letters followed by one letter which is not x or X, [a-zA-Z]{5}[a-wy-zA-WY-Z] ([^xX] would match numers too or anything else).
The 3rd alternative is slightly more complex: six letters or digits, which is not preceded by 000; this uses a negative lookahead, and it works because of the anchor ^ (if you remove that, it breaks).
The 1st alternative is similar: zero or more digits, followed by one or more non-0 digits, followed by zero or more digits; all not starting by 7 or more characters.
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+)+";
I need to replace all non-digit charaters in the string. For instance:
String: 987sdf09870987=-0\\\`42
Replaced: 987**sdf**09870987**=-**0**\\\`**42
That's all non-digit char-sequence wrapped into ** charaters. How can I do that with String::replaceAll()?
(?![0-9]+$).*
the regex doesn't match what I want. How can I do that?
(\\D+)
You can use this and replace by **$1**.See demo.
https://regex101.com/r/fM9lY3/2
You can use a negated character class for a non-digit and use the 0th group back-reference to avoid overhead with capturing groups (it is minimal here, but still is):
String x = "987sdf09870987=-0\\\\\\`42";
x = x.replaceAll("[^0-9]+", "**$0**");
System.out.println(x);
See demo on IDEONE. Output: 987**sdf**09870987**=-**0**\\\`**42.
Also, in Java regex, character classes look neater than multiple escape symbols, that is why I prefer this [^0-9]+ pattern meaning match 1 or more (+) symbols other than (because of ^) digits from 0 to 9 ([0-9]).
A couple of words about your (?![0-9]+$).* regex. It consists of a negative lookahead (?![0-9]+$) that checks if from the current position onward there are no digits only (if there are only digits up to the end of string, the match fails), and .* matching any characters but a newline. You can see example of what it is doing here. I do not think it can help you since you need to actually match non-numbers, not just check if digits are absent.
I have a string such as:
file = "UserTemplate324.txt"
I'd like to extract "324". How can I do this without any external libraries like Apache StringUtils?
Assuming you want "the digits just before the dot":
String number = str.replaceAll(".*?(\\d+)\\..*", "$1");
This uses regex to find and capture the digits and replace the entire input with them.
Of minor note is the use of a non-greedy quantifier to consume the minimum leading input (so as not to consume the leading part of the numbers too).
If you want to exclude the non-digit characters:
String number = file.replaceAll("\\D+", "");
\\D+ means a series of one or more non digit (0-9) characters and you replace any such series by "" i.e. nothing, which leaves the digits only.
I have the following expression where i want to extract an identifier that is 12 digits long:
([12]\d{3})(\d{6})(\d{2})
This works fine if the string is in the following format:
ABCD123456789101
123456789101
When it gets a string like the following, how does it know which 12 digits to match on:
ABCD1234567894837376383439434343232
1234567894837376383439434343232
In the above scenario, i dont want to select the twelve digits. So the answer i think is to only select the twelve digits, if those twelve digits are not preceded or proceeded by other digits. I tried this change:
[^0-9]([12]\d{3})(\d{6})(\d{2})[^0-9]
This basically says get me the 12 digits only if the characters before and after the 12 digits are non numeric. The problem i have is i am also getting those non-numeric characters as part of the match i.e.
ABCD123456789483X7376383439434343232 returns D123456789483X
Is there anyway of checking what the preceding and proceeding characters are but not include them in the match result? i.e. only match if the preceding and proceeding characters are non numeric but don't include those non-numeric characters in the match result.
You can use lookarounds:
(?<!\\d)([12]\d{3})(\d{6})(\d{2})(?!\\d)
Here:
(?<!\\d) is a negative lookbehind which means your pattern is not preceded by a digit
(?!\\d) is a negative lookahead which means your pattern is not followed by a digit
Read more about lookarounds