Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式匹配单词的一部分_Java_Regex - Fatal编程技术网

Java 正则表达式匹配单词的一部分

Java 正则表达式匹配单词的一部分,java,regex,Java,Regex,我想匹配单词的一部分,如果模式中单词的长度小于我要匹配的字符串,则此操作有效,例如: Pattern p = Pattern.compile("Stude"); Matcher m = p.matcher("Student"); System.out.println(m.find()) Pattern p = Pattern.compile("Studentsrter"); Matcher m = p.matcher("Student"); System.out.println(m.fin

我想匹配单词的一部分,如果模式中单词的长度小于我要匹配的字符串,则此操作有效,例如:

Pattern p = Pattern.compile("Stude");
Matcher m = p.matcher("Student");

System.out.println(m.find())
Pattern p = Pattern.compile("Studentsrter");
Matcher m = p.matcher("Student");

System.out.println(m.find())
输出为true。但是,如果单词长度较大,则返回false,例如:

Pattern p = Pattern.compile("Stude");
Matcher m = p.matcher("Student");

System.out.println(m.find())
Pattern p = Pattern.compile("Studentsrter");
Matcher m = p.matcher("Student");

System.out.println(m.find())
那么,我怎样才能只匹配单词的一部分呢?

这个呢

Pattern p = Pattern.compile("(Student).*");

将匹配Student后跟其他任何内容。

您可以使用此模式
(?i)。*(Student)。*
,如

Pattern p = Pattern.compile("(?i).*(student).*");
Matcher m = p.matcher("asaStudentstrtr");
其中:

(?i)
使其不区分大小写

*
表示0个或更多字符

(学生)
是您想要查找的具体字符组


出于您的目的,您可以删除
(?i)
,使其区分大小写,或者在模式的开头或结尾删除
*
,以确定所需单词在字符串中的位置。

是否有必须匹配的最小值?例如,将
“S”
视为与
“Studentsrter”
匹配吗?可能
模式p=Pattern.compile(“Student\w*”)