使用正则表达式从字符串中提取所需的单词
Extraction of required words from a string using regular expression
if (txt.matches("Who is the(.*)")) {
String re1=".*?";
String re2="(?:[a-z][a-z]+)";
String re3=".*?";
String re4="(?:[a-z][a-z]+)";
String re5=".*?";
String re6="(?:[a-z][a-z]+)";
String re7=".*?";
String re8="((?:[a-z][a-z]+))";
String re9=".*?";
String re10="(?:[a-z][a-z]+)";
String re11=".*?";
String re12="((?:[a-z][a-z]+))";
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String word1=m.group(1);
String word2=m.group(2);
String z=word2.toString()+"&&"+word1.toString();
System.out.println(z);
}
}
我从网上获得了这段代码,但不理解它如何从字符串中提取少数必需单词的代码 "txt"。特别是 re1 和 re2 是什么……等等。对于任何东西-"Who is the HOD of ECE"它returns ECE&&HOD。有人可以解释一下代码吗...请帮忙。
查看代码中的注释以解释此代码的作用。 re1
和 re2
所做的只是创建一个字符串,然后将其加入代码的 Patern.compile()
部分。当你有 2 个字符串,如 String foo = "foo"
和 String bar = "bar"
并且你执行 String foobar = foo + bar;
时,输出是 foobar
。
//If the String object "txt" matches the given regex, which in this case is "Who is the(.*)"
if (txt.matches("Who is the(.*)")) {
String re1=".*?";
String re2="(?:[a-z][a-z]+)";
String re3=".*?";
String re4="(?:[a-z][a-z]+)";
String re5=".*?";
String re6="(?:[a-z][a-z]+)";
String re7=".*?";
String re8="((?:[a-z][a-z]+))";
String re9=".*?";
String re10="(?:[a-z][a-z]+)";
String re11=".*?";
String re12="((?:[a-z][a-z]+))";
//this creates a pattern using the above strings.
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
//creates matcher object from the String object "txt"
Matcher m = p.matcher(txt);
//m.find() "Attempts to find the next subsequence of the input sequence that matches the pattern.", if it does...
if (m.find())
{
//m.group "Returns the input subsequence matched by the previous match."
String word1=m.group(1);
String word2=m.group(2);
//creates a string
String z=word2.toString()+"&&"+word1.toString();
//prints out string
System.out.println(z);
}
}
if (txt.matches("Who is the(.*)")) {
String re1=".*?";
String re2="(?:[a-z][a-z]+)";
String re3=".*?";
String re4="(?:[a-z][a-z]+)";
String re5=".*?";
String re6="(?:[a-z][a-z]+)";
String re7=".*?";
String re8="((?:[a-z][a-z]+))";
String re9=".*?";
String re10="(?:[a-z][a-z]+)";
String re11=".*?";
String re12="((?:[a-z][a-z]+))";
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String word1=m.group(1);
String word2=m.group(2);
String z=word2.toString()+"&&"+word1.toString();
System.out.println(z);
}
}
我从网上获得了这段代码,但不理解它如何从字符串中提取少数必需单词的代码 "txt"。特别是 re1 和 re2 是什么……等等。对于任何东西-"Who is the HOD of ECE"它returns ECE&&HOD。有人可以解释一下代码吗...请帮忙。
查看代码中的注释以解释此代码的作用。 re1
和 re2
所做的只是创建一个字符串,然后将其加入代码的 Patern.compile()
部分。当你有 2 个字符串,如 String foo = "foo"
和 String bar = "bar"
并且你执行 String foobar = foo + bar;
时,输出是 foobar
。
//If the String object "txt" matches the given regex, which in this case is "Who is the(.*)"
if (txt.matches("Who is the(.*)")) {
String re1=".*?";
String re2="(?:[a-z][a-z]+)";
String re3=".*?";
String re4="(?:[a-z][a-z]+)";
String re5=".*?";
String re6="(?:[a-z][a-z]+)";
String re7=".*?";
String re8="((?:[a-z][a-z]+))";
String re9=".*?";
String re10="(?:[a-z][a-z]+)";
String re11=".*?";
String re12="((?:[a-z][a-z]+))";
//this creates a pattern using the above strings.
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
//creates matcher object from the String object "txt"
Matcher m = p.matcher(txt);
//m.find() "Attempts to find the next subsequence of the input sequence that matches the pattern.", if it does...
if (m.find())
{
//m.group "Returns the input subsequence matched by the previous match."
String word1=m.group(1);
String word2=m.group(2);
//creates a string
String z=word2.toString()+"&&"+word1.toString();
//prints out string
System.out.println(z);
}
}