对不起,我对Regex还不熟悉,但是我似乎无法用任何我已经尝试过的正则表达式来实现下面的目标。
我们对“单词”感兴趣(也就是说,这个单词完全是字母,只包含上、下或混合大小写的字母)。所有其他内容都被忽略)
我尝试使用的一个示例字符串如下:
要找到金币,你必须买一块巧克力:)查理的奶奶和爷爷都希望他能买到一张票,但他只有足够的钱买一个酒吧。我印了5张票,但我的工人做了1000000多个酒吧:)
因此,像Charlie,Oompa和笑脸这样的词不应该包含在输出中。只是字母全字母的单词。
我尝试使用其他问题中的一些例子,例如这个here试图使用Regex的例子,比如^ Any +(‘Any+)$,但不幸的是,正如我前面所说的,我对Regex并不熟悉,所以我不太确定我在做什么。如果有任何帮助,我们将不胜感激。
发布于 2016-04-26 03:57:58
描述
此正则表达式将执行以下操作:
Regex
(?<=\s|^)[a-zA-Z]*(?=[.,;:]?\s|$)

解释
NODE EXPLANATION
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
^ start of the string
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
[a-zA-Z]* any character of: 'a' to 'z', 'A' to 'Z'
(0 or more times (matching the most amount
possible))
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[.,;:]? any character of: '.', ',', ';', ':'
(optional (matching the most amount
possible))
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------示例
联机Regex演示
http://fiddle.re/65eqna
示例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("(?<=\\s|^)[a-zA-Z]*(?=[.,;:]?\\s|$)");
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] => To
[1] => find
[2] => the
[3] => golden
[4] => ticket
[5] => you
[6] => have
[7] => to
[8] => buy
[9] => a
[10] => bar
[11] => of
[12] => chocolate
[13] => Granny
[14] => and
[15] => Grandad
[16] => are
[17] => hoping
[18] => he
[19] => gets
[20] => a
[21] => ticket
[22] => but
[23] => he
[24] => only
[25] => has
[26] => enough
[27] => money
[28] => to
[29] => buy
[30] => bar
[31] => I
[32] => printed
[33] => tickets
[34] => but
[35] => my
[36] => workers
[37] => made
[38] => more
[39] => than
[40] => bars
)
)发布于 2016-04-25 21:58:49
您可以使用:
words.split("[ ]+");然后,对于该数组中的每个字符串,如果它符合您的条件,则如下所示:
str.matches("[a-zA-Z]+");https://stackoverflow.com/questions/36851740
复制相似问题