i have a string for ex- "Abc Xyz Def OSSV-1810-017466-AB01 Def Utv" How to write a regex that mathches formats like "OSSV-1810-017466-AB01" and extract only that from any given string?
What is tried:
public class RegexTest {
public static void main(String[] args) {
String str = "Abc Xyz Def OSSV-1810-017466-AB01 Def Utv";
Pattern pattern = Pattern.compile("[a-zA-Z\\-\\0-9\\-\\0-9\\-\\^A-Za-z0-9]");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.toString());
if(matcher.find()){
System.out.println("found" + matcher.group(1));
}
else{
System.out.println("not found");
}
}
}
You can use this pattern [AZ]{4}-\\\\d{4}-\\\\d{6}-[AZ]{2}\\\\d{2}
.
Also change matcher.group(1)
to matcher.group(0)
.
public static void main(String[] args) {
String input="Abc Xyz Def OSSV-1810-017466-AB01 Def Utv";
String regEx="([a-zA-Z ]*)((?:[A-Z]{4}-)(?:[0-9]{4}-)(?:[0-9]{6}-)(?:[A-Z0-9]{4}))([a-zA-Z ]*)";
Matcher matcher = Pattern.compile(regEx).matcher(input);
if(matcher.matches()) {
System.out.println(matcher.group(2));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.