[英]How to write a regex in java to check whether a string contains two chars space and three integers?
I have a string String myString = "abc,QWAB 123,cdef";
我有一个字符串
String myString = "abc,QWAB 123,cdef";
. 。
How to write regex to check whether my string has AB (space)123. 如何编写正则表达式以检查我的字符串是否具有AB(空格)123。 I don't want to consider
QW
. 我不想考虑
QW
。 Also number 123
will be one digit or two digit or three digit, Always it will not be three digit. 同样,数字
123
将是一位数字,两位数字或三位数字,始终不是三位数字。
Please help. 请帮忙。
Pattern pat = Pattern.compile( "AB \\d{1,3}" );
^[^,]+,AB \d{1,3},.*$
Try this.This should do it.See demo. 试试这个,这应该做到的。
https://regex101.com/r/gX5qF3/8 https://regex101.com/r/gX5qF3/8
Try something like: 尝试类似:
String myString = "abc,QWAB 123,cdef";
if (myString.matches(".*AB [0-9]{1,3}.*")) {
System.out.println("Yes it mateched");
} else {
System.out.println("Hard luck");
}
This this: 这个:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Hello world!
*
*/
public class App {
static String myString = "abc,QWAB 123,cdef";
static String abcPattern = "(AB[\\s]{1}\\d{1,3})";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(App.abcPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(myString);
if (matcher.find()) {
System.out.println(matcher.group());
} else {
System.out.println(" No mach found ");
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.