简体   繁体   English

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

[英]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.

相关问题 检查List / ArrayList是否包含字符串Java的字符 - Check if a List/ArrayList contains chars of a string Java java中如何判断一个字符串是否至少包含一个字母? - How to check whether a string contains at least one alphabet in java? 如何在Java中使用正则表达式检查字符串是否包含两个字母和数量可变的数字? - How to check with a regex if a String contains two letters and a variable amount of digits in Java? 如何在Java中的字符串中找到以空格分隔的字符? - How to find space separated chars in a string in java? 如何检查字符串是否包含搜索项 - How to check whether a string contains the search items 使用 Java Regex,如何检查字符串是否包含集合中的任何单词? - Using Java Regex, how to check if a string contains any of the words in a set ? 使用 Java 正则表达式,如何检查字符串是否包含任何“|” 但不是“||” - Using Java Regex, how to check if a String contains any "|" but not "||" 检查字符串是否只包含字母空格和引号(最好不包含正则表达式) - check whether a string contains only letters spaces and quotes (preferably no regex) Java Regex检查字符串是否包含XML标记 - Java Regex check if string contains XML tag Java正则表达式检查字符串是否包含1个字符(数字)> 0 - Java regex check if string contains 1 character (number) > 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM