首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >只适用于字母字符的正则表达式- Java

只适用于字母字符的正则表达式- Java
EN

Stack Overflow用户
提问于 2016-04-25 21:51:56
回答 2查看 18.9K关注 0票数 2

对不起,我对Regex还不熟悉,但是我似乎无法用任何我已经尝试过的正则表达式来实现下面的目标。

我们对“单词”感兴趣(也就是说,这个单词完全是字母,只包含上、下或混合大小写的字母)。所有其他内容都被忽略)

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

要找到金币,你必须买一块巧克力:)查理的奶奶和爷爷都希望他能买到一张票,但他只有足够的钱买一个酒吧。我印了5张票,但我的工人做了1000000多个酒吧:)

因此,像Charlie,Oompa和笑脸这样的词不应该包含在输出中。只是字母全字母的单词。

我尝试使用其他问题中的一些例子,例如这个here试图使用Regex的例子,比如^ Any +(‘Any+)$,但不幸的是,正如我前面所说的,我对Regex并不熟悉,所以我不太确定我在做什么。如果有任何帮助,我们将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-26 03:57:58

描述

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

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

Regex

代码语言:javascript
运行
复制
(?<=\s|^)[a-zA-Z]*(?=[.,;:]?\s|$)

解释

代码语言:javascript
运行
复制
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代码

代码语言:javascript
运行
复制
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++;
    }
  }
}

示例捕获

代码语言:javascript
运行
复制
$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
        )

)
票数 11
EN

Stack Overflow用户

发布于 2016-04-25 21:58:49

您可以使用:

代码语言:javascript
运行
复制
words.split("[ ]+");

然后,对于该数组中的每个字符串,如果它符合您的条件,则如下所示:

代码语言:javascript
运行
复制
str.matches("[a-zA-Z]+");
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36851740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档