.
Jun 18, 2020
Assassins Creed 2. 9 CloneDVD PC DVD9 [WITH CRACK]. Action / Adventure 2010 1 DVD ISO Windows Crack
Q:
Java Regex: how to match 2 or more characters?
Given a string, I want to do a regex on it.
This is my current code:
final Pattern pattern = Pattern.compile("[A-Z][0-9]+");
final Matcher matcher = pattern.matcher(s);
It works for strings such as 12345, but when I add an extra character like so:
final String s2 = "12345678";
If I then use my current regex, it will match 12345678, but I want to match 1234567.
I know I can use \d{2} or even \d{2,4} but how can I combine these to make it so it matches 2 or more characters?
I also want to match 0 or more non-alpha characters.
I tried combining these like so:
final Pattern pattern = Pattern.compile("[A-Z][0-9]\d{2}[^A-Z]");
But I get an error from the console that tells me:
"Unknown or invalid modifier '^'".
A:
It seems you may be able to use alternation to accomplish your desired matching. I'm not positive, but you can also use a non-capturing group.
Pattern: ([A-Z][0-9]{2})|([^A-Z]+)
(...) Represents non-capturing group.
Alternative:
Pattern: ([A-Z][0-9]{2})+
(...) Represents non-capturing group.
With either of these, your input might look like this:
String in = "12345678";
Pattern p = Pattern.compile(
"[A-Z][0-9]{2}|([^A-Z]+)");
Matcher m = p.matcher(in);
while(m.find()){
// Matches 1 to 2 digits, 2 letters, or 1 to any non-alphanumeric character.
}
System.out.println(m.group(1));
Pattern p2 be359ba680
Related links:
Comments