**********menu 1************
gaurav
saurav
amit
avanish
**********menu 2************
gauravqwe
sourav
anit
abhishek
现在我要检查项目" gaurav"从菜单1或菜单2开始。
如果" gaurav"从菜单1然后返回true,否则返回false
我试试:
class Regex_Test {
public void checker(String Regex_Pattern){
Scanner Input = new Scanner(System.in);
String Test_String = Input.nextLine();
Pattern p = Pattern.compile(Regex_Pattern);
Matcher m = p.matcher(Test_String);
System.out.println(m.find());
}
}
public class CheckHere {
public static void main(String[] args) {
Regex_Test tester = new Regex_Test();
tester.checker("^[gsa][amv]"); // Use \\ instead of using \
}
}
但是在" gauravqwe"
的情况下它返回true
我需要表达" string"对于上述问题
条件字符串大小小于15个字符
答案 0 :(得分:0)
答案 1 :(得分:0)
要在少于15个字符的情况下执行此操作,您需要一个如下所示的正则表达式:
^gaurav(\r|\Z)
此正则表达式是以下答案的子集。
我通过构建正则表达式获取匹配条目的菜单标题,一次性完成。这个正则表达式将执行以下操作:
gaurav
正则表达式
[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\r|\Z)
请注意,此正则表达式使用以下选项:不区分大小写,多行(使用^和$匹配开始和结束行),并且点匹配新行(使用。匹配\ n)
<强>解释强>
这个构造(?:(?![*]{9,}).)*
是所有魔法发生的地方。这会强制搜索在字符串中向前移动,但不允许模式匹配跨越多个**********
分隔的段。
^
和(?:\n|\Z)
构造强制正则表达式引擎匹配完整字符串,而不仅仅是初始字符。示例:如果您正在寻找gaurav
,那么gauravqwe
将无法匹配。
NODE EXPLANATION
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9 times
(matching the most amount possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^*]*? any character except: '*' (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9 times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
gaurav 'gaurav'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\r '\r' (carriage return)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\Z before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
Java代码示例
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\\r|\\Z)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
<强>返回强>
$matches Array:
(
[0] => Array
(
[0] => **********menu 1************
gaurav
)
[1] => Array
(
[0] => menu 1
)
)