Java 如何检查以foo或bar开头的行?
就我而言,我有一个词,比如Java 如何检查以foo或bar开头的行?,java,regex,Java,Regex,就我而言,我有一个词,比如 **********menu 1************ gaurav saurav amit avanish **********menu 2************ gauravqwe sourav anit abhishek 现在我想检查菜单一或菜单二的“gaurav”项 如果菜单1中的“gaurav”,则返回true,否则返回false 我尝试: class Regex_Test { public void checker(String Reg
**********menu 1************
gaurav
saurav
amit
avanish
**********menu 2************
gauravqwe
sourav
anit
abhishek
现在我想检查菜单一或菜单二的“gaurav”项
如果菜单1中的“gaurav”,则返回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个字符使用元字符单词边界
\b
\bbgaurav\b
参考资料:
使用元字符单词边界
\b
\bbgaurav\b
参考资料: 少于15个字符 要在少于15个字符内完成此操作,您需要一个如下所示的正则表达式:
^gaurav(\r|\Z)
这个正则表达式是下面答案的子集
描述
我会通过构建一个正则表达式来获取匹配条目的菜单标题,一次完成这项工作。此正则表达式将执行以下操作:
- 在源字符串中查找
gaurav
- 从匹配部分返回菜单名
- 如果字符串不包含匹配项,则返回值将为空
[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\r|\Z)
注意:此正则表达式使用以下选项:不区分大小写、多行(带“^”和“$”匹配行首和行尾),以及点匹配新行(带.matching\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++;
}
}
}
少于15个字符
要在少于15个字符内完成此操作,您需要一个如下所示的正则表达式:
^gaurav(\r|\Z)
这个正则表达式是下面答案的子集
描述
我会通过构建一个正则表达式来获取匹配条目的菜单标题,一次完成这项工作。此正则表达式将执行以下操作:
- 在源字符串中查找
gaurav
- 从匹配部分返回菜单名
- 如果字符串不包含匹配项,则返回值将为空
[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\r|\Z)
注意:此正则表达式使用以下选项:不区分大小写、多行(带“^”和“$”匹配行首和行尾),以及点匹配新行(带.matching\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++;
}
}
}
花点时间让你的问题更精确,让你的示例代码更有意义。你可以编辑你的问题。这种行为是正确的,因为gauravqwe也以ga开头。你的问题是什么?如果你想用正则表达式来解决这个问题,你必须使用不同的模式。现在我想从菜单一或菜单二中选择“gaurav”项。如果菜单1中的“gaurav”,则返回true,否则返回false此奇怪字符串来自哪里:“^[gsa][amv]”?为什么你认为它能帮助你解决问题?花点时间让你的问题更精确,让你的示例代码更有意义。你可以编辑你的问题。这种行为是正确的,因为gauravqwe也以ga开头。你的问题是什么?如果你想用正则表达式来解决这个问题,你必须使用不同的模式。现在我想从菜单一或菜单二中选择“gaurav”项。如果菜单1中的“gaurav”,则返回true,否则返回false此奇怪字符串来自哪里:“^[gsa][amv]”?为什么你认为它对你的问题有帮助?实际上,我需要表达式“string”来回答上述问题,字符串大小小于15个字符。实际上,我需要表达式“string”来回答上述问题,字符串大小小于15个字符。