繁体   English   中英

如何在Java中编写正则表达式以检查字符串是否包含两个字符空间和三个整数?

[英]How to write a regex in java to check whether a string contains two chars space and three integers?

我有一个字符串String myString = "abc,QWAB 123,cdef";

如何编写正则表达式以检查我的字符串是否具有AB(空格)123。 我不想考虑QW 同样,数字123将是一位数字,两位数字或三位数字,始终不是三位数字。

请帮忙。

Pattern pat = Pattern.compile( "AB \\d{1,3}" );
^[^,]+,AB \d{1,3},.*$

试试这个,这应该做到的。

https://regex101.com/r/gX5qF3/8

尝试类似:

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");
}

这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM