如何在 Java 中使用 Regex 进行模式匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14862289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Regex in Java to pattern match?
提问by PAujla03
I have read the documentation and various tutorials online but I'm still confused on how regex works in Java. What I am trying to do is create a function which takes in argument of type string. I then want to check if the passed string contains any characters other than MDCLXVIivxlcdm. So for example, string "XMLVID" should return false and "ABXMLVA" should return true.
我已经在线阅读了文档和各种教程,但我仍然对正则表达式在 Java 中的工作方式感到困惑。我想要做的是创建一个函数,它接受字符串类型的参数。然后我想检查传递的字符串是否包含除 MDCLXVIivxlcdm 之外的任何字符。例如,字符串“XMLVID”应该返回false,而“ABXMLVA”应该返回true。
public boolean checkString(String arg)
{
Pattern p = Pattern.complile("[a-zA-z]&&[^MDCLXVIivxlcdm]");
Matcher m = p.matcher(arg);
if(m.matches())
return true;
else
return false;
}
When I pass, "XMLIVD", "ABXMLVA", and "XMLABCIX", all return false. What am I doing wrong? Any help will be greatly appreciated.
当我通过“XMLIVD”、“ABXMLVA”和“XMLABCIX”时,都返回false。我究竟做错了什么?任何帮助将不胜感激。
回答by Bergi
You will need to use Java's character classintersection operator inside a character class, otherwise it literally matches &&
. Btw, your first character class from A
to (lowercase) z
also includes [\]^_
, which you certainly do not want; and you misspelled "Patter.complile".
您将需要在字符类中使用Java 的字符类交集运算符,否则它会直接匹配&&
. 顺便说一句,您从A
to(小写)的第一个字符类z
还包括[\]^_
您肯定不想要的;你拼错了“Patter.complile”。
Also, matches()
还, matches()
Attempts to match the entireregion against the pattern.
尝试将整个区域与模式匹配。
So you either need to use find()
instead or pad the expression with .*
.
所以你要么需要find()
改用,要么用.*
.
public boolean checkString(String arg) {
return Pattern.compile("[[a-zA-Z]&&[^MDCLXVIivxlcdm]]").matcher(arg).find();
}
回答by Arundev
you can use a function like this, with two arguments, viz.,
你可以使用这样的函数,有两个参数,即,
origingalString
the original string to checksearchString
the string to be searched
origingalString
要检查的原始字符串searchString
要搜索的字符串
the code exactly,
确切的代码,
public boolean checkCompletelyExist(String origingalString,String searchString){
boolean found = false;
String regex = "";
try{
for(int i = 0; i < searchString.length();i++){
String temp = String.valueOf(searchString.charAt(i));
regex = "[\x20-\x7E]*"+"["+temp.toLowerCase()+"|"+temp.toUpperCase()+"]+[\x20-\x7E]*";
if(!origingalString.matches(regex)){
found = true;
break;
}
}
System.out.println("other character present : "+found);
} catch (Exception e) {
e.printStackTrace();
}
return found;
}
eg:
例如:
checkCompletelyExist("MDCLXVIivxlcdm","XMLVID")
output will be other character present : false
checkCompletelyExist("MDCLXVIivxlcdm","XMLVID")
输出将是 other character present : false
and
和
checkCompletelyExist("MDCLXVIivxlcdm","ABXMLVA")
output will be other character present : true
checkCompletelyExist("MDCLXVIivxlcdm","ABXMLVA")
输出将是 other character present : true
回答by Yash
Regular Expressions(RegEx / RegExp)Basically, a regular expression is a pattern describing a certain amount of text.
正则表达式(RegEx / RegExp)基本上,正则表达式是描述一定数量文本的模式。
^abc$ start / end of the string
\b \B word, not-word boundary
\w \d \s word, digit, whitespace
\W \D \S not word, digit, whitespace
\z - End of entire string
(…) - Grouping (capture groups)
[abc] any of a, b, or c
[^abc] not a, b, or c
[a-g] character between a & g
{ m,n } - quantifiers for “from m to n repetitions”
+ - quantifiers for 1 or more repetitions (i.e, {1,})
? - quantifiers for 0 or 1 repetitions (i.e, {0,1})
POSIX Bracket Expressions
POSIX bracket expressions are a special kind of character classes. POSIX bracket expressions match one character out of a set of characters, just like regular character classes. The POSIX character class names must be written all lowercase. The POSIX standard defines 12 character classes. The table below lists all 12, plus the [:ascii:]
and [:word:]
classes that some regex flavors also support.
POSIX Bracket Expressions
POSIX 括号表达式是一种特殊的字符类。POSIX 括号表达式匹配一组字符中的一个字符,就像常规字符类一样。POSIX 字符类名称必须全部小写。POSIX 标准定义了 12 个字符类。下表列出了所有 12 个,以及一些正则表达式风格也支持的[:ascii:]
和[:word:]
类。
Pattern Matching withRegular Expressions:
模式匹配与正则表达式:
final Pattern mix_alphaNumaric_pattern = Pattern.compile("^[A-Za-z0-9]+$");
final Pattern alphabets_pattern = Pattern.compile("^[A-Za-z,\- ]+$");
final Pattern alphabetsNull_pattern = Pattern.compile("|^[A-Za-z,\- ]+$");
final Pattern numaric_pattern = Pattern.compile("^[0-9]+$"); // ^begning +followed By $end
final Pattern date_time_pattern = Pattern.compile("\d{1,2}/\d{1,2}/\d{4}\s\d{1,2}\:\d{1,2}");
final Pattern weather_pattern = Pattern.compile("[\-]*\d{1,3}\.\d{1,6}");
final Pattern email_pattern = Pattern.compile("^[\w-_\.+]*[\w-_\.]\@([\w]+\.)+[\w]+[\w]$");
final Pattern mobile_pattern = Pattern.compile("(\+)?(\d{1,2})?\d{10}");
public static void main(String[] args) {
String[] str = {"MCDL", "XMLIVD", "ABXMLVA", "XMLABCIX"};
Pattern p = Pattern.compile("^(M|D|C|L|X|V|I|i|v|x|l|c|d|m)+$");
// Returns: true if, and only if, the entire region sequence matches this matcher's pattern
for (String sequence : str ) {
boolean match = false, find = false;
if ( !p.matcher(sequence).matches() ) match = true;
if (p.matcher(sequence).find()) find = true;
System.out.format("%s \t Match[%s] Find[%s]\n", sequence, match, find);
}
}
OutPut:
输出:
MCDL Match[false] Find[true]
XMLIVD Match[false] Find[true]
ABXMLVA Match[true] Find[false]
XMLABCIX Match[true] Find[false]
@see this links to:
@看到这个链接到: