gpt4 book ai didi

java - 仅适用于字母字符的正则表达式 - Java

转载 作者:行者123 更新时间:2023-12-03 03:34:27 27 4
gpt4 key购买 nike

抱歉,我是正则表达式的新手,但我似乎无法使用迄今为止尝试过的任何正则表达式来实现以下目标。

我们对“单词”感兴趣(即该单词完全按字母顺序排列,仅包含大写、小写或混合大小写的字母。所有其他内容都将被忽略)

我尝试使用的示例字符串如下:

要找到金奖券,你必须买一 block 巧克力:)查理的奶奶和爷爷希望他能得到一张奖券,但他的钱只够买一根巧克力。我打印了 5 张票,但我的 Oompa-Loompa worker 制作了超过 1000000 个金条:)

因此像 Charlie's、Oompa-Loompa 和笑脸这样的词不应该包含在输出中。只是完全按字母顺序排列的单词。

我尝试使用其他问题中的一些示例,例如这个 here尝试使用正则表达式,例如 ^[a-zA-Z]+('[a-zA-Z]+)?$ 但不幸的是,正如我之前所说,我是正则表达式的新手,所以我'我不太确定我在做什么。任何帮助,将不胜感激。

最佳答案

描述

此正则表达式将执行以下操作:

  • 假设单词完全由字母字符 A-Z、大写和小写组成
  • 查找所有单词
  • 忽略所有包含非字母字符或符号的字符串
  • 假设忽略一些标点符号(例如句点或逗号),但应捕获前面的单词。

正则表达式

(?<=\s|^)[a-zA-Z]*(?=[.,;:]?\s|$)

Regular expression visualization

说明

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
----------------------------------------------------------------------

示例

在线正则表达式演示

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
)

)

关于java - 仅适用于字母字符的正则表达式 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com