Java 查找字符串中所有 3 个字符长度的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18501538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:37:17  来源:igfitidea点击:

Finding all 3 character length substrings in a string

javaregex

提问by MLD_Saturn

I am trying to find all three letter substrings from a string in Java.

我正在尝试从 Java 中的字符串中查找所有三个字母子字符串。

For example from the string "example string" I should get "exa", "xam", "amp", "mpl", "ple", "str", "tri", "rin", "ing".

例如,从字符串“example string”我应该得到“exa”、“xam”、“amp”、“mpl”、“ple”、“str”、“tri”、“rin”、“ing”。

I tried using the Java Regular expression "([a-zA-Z]){3}" but I only got "exa", "mpl", "str", "ing".

我尝试使用 Java 正则表达式“([a-zA-Z]){3}”,但我只有“exa”、“mpl”、“str”、“ing”。

Can someone tell me a regex or method to correct this.

有人可以告诉我一个正则表达式或方法来纠正这个问题。

采纳答案by jpmc26

Implementing Juvanis' idea somewhat, iterate to get your substrings, then use a regular expression to make sure the substring is all letters:

在某种程度上实现 Juvanis 的想法,迭代以获取您的子字符串,然后使用正则表达式来确保子字符串都是字母:

String s = "example string";
for (int i = 0; i <= s.length() - 3; i++) {
    String substr = s.substring(i, i + 3);
    if (substr.matches("[a-zA-Z]+")) { System.out.println(substr); }
}

回答by Juvanis

When a character is consumed in one regex, it cannot be used in other regexes. In your example, ais consumed in exaso ampwill not be listed as output. You should try traditional iterative approach. It is easier to implement.

当一个字符在一个正则表达式中被消耗时,它不能在其他正则表达式中使用。在您的示例中,aexa 中消耗,因此amp不会被列为输出。您应该尝试传统的迭代方法。它更容易实施。

回答by Evgeniy Dorofeev

try this

尝试这个

    Matcher m = Pattern.compile("([a-zA-Z]){3}").matcher("example string");
    for (int i = 0; m.find(i); i = m.start() + 1) {
        System.out.print(m.group() + " ");
    }

output

输出

exa xam amp mpl ple str tri rin ing 

回答by Roney Michael

This can be done using regex as follows:

这可以使用正则表达式完成,如下所示:

  1. Find the position of all matches for the string using the regex \w(?=\w\w). This will give you the start index of the first character of each required sub-string.

    In this case, you would get: 0, 1, 2, 3, 4, 8, 9, 10and 11.

  2. Get what you need by taking the sub-strings starting from each position going upto that plus 2.

    In this case, that would mean, my_string.substring(0,3), my_string.substring(1,4)and so on, as the begin index parameter is inclusive while the end index parameter is exclusive.

  1. 使用 regex 查找字符串的所有匹配项的位置\w(?=\w\w)。这将为您提供每个所需子字符串的第一个字符的起始索引。

    在这种情况下,您会得到:0, 1, 2, 3, 4, 8, 9,1011

  2. 通过从每个位置开始到加 2 的子字符串来获取您需要的内容。

    在这种情况下,这将意味着,my_string.substring(0,3)my_string.substring(1,4)等,作为开始索引参数是包容而结束索引参数是唯一的。