我需要一个正则表达式来确定字符串是以分号结尾还是以“开始”或“然后”结尾。此外,在开始之前,然后单词,必须有一个空白或行中断字符。
if(strLineText.matches(";|THEN|BEGIN$"))这对于那时和开始都是有效的,但对分号不起作用。同样地,用这个正则表达式,我无法确定当时和开始是否是确切的单词。
发布于 2015-11-16 10:11:10
你得把他们放进一个小组里。
if(strLineText.matches("(?s).*(?:;|\\bTHEN|\\bBEGIN)$"))或
if(strLineText.matches("(?s).*(?:;|\\sTHEN|\\sBEGIN)$"))发布于 2015-11-16 10:11:35
对于同样的情况,可以使用简单的前瞻性。
^(?=.*(?:;|[ \\n]THEN|[ \\n]BEGIN)$).*$发布于 2015-11-16 10:21:11
这不是裁判官。
您也可以使用.endsWith()方法。
String str = "hey;";
if(str.endsWith(";"))
System.out.println("Ends with a ;");public static boolean endsWithWord(String str, String word)
{
return str.endsWith(word);
}
System.out.println(endsWithWord("hey;", ";"));
System.out.println(endsWithWord("umm BEGIN", "BEGIN"));
System.out.println(endsWithWord("umm THEN", "THEN"));https://stackoverflow.com/questions/33732787
复制相似问题