如何匹配重复模式? [英] How to match repeated patterns?

查看:105
本文介绍了如何匹配重复模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要匹配:

some.name.separated.by.dots

但我不知道怎么做。

我可以匹配这样的单个部分

I can match a single part like this

 \w+\.

我怎么说重复那个

推荐答案

请尝试以下方法:

\w+(\.\w+)+

<$ c后 + $ c>(...)告诉它与括号内的内容匹配一次或多次。

The + after ( ... ) tell it to match what is inside the parenthesis one or more times.

注意 \w 仅匹配ASCII字符,因此像café这样的单词不会与 \w +匹配,更不用说包含Unicode的单词/文字。

Note that \w only matches ASCII characters, so a word like café wouldn't be matches by \w+, let alone words/text containing Unicode.

之间的区别 [...] (...) [...] 始终匹配单个字符。它被称为字符集或字符类。所以, [abc] 匹配字符串abc,但匹配一个字符 a b c

The difference between [...] and (...) is that [...] always matches a single character. It is called a "character set" or "character class". So, [abc] does not match the string "abc", but matches one of the characters a, b or c.

\w + [\.\w +] * 的事实也与你的字符串匹配,因为 [\.\w +] 匹配或来自 \w ,然后由 * 重复零次或多次。但是, \w + [\.\w +] * 也会匹配 aaaaa 或<$等字符串c $ c> aaa ...........

The fact that \w+[\.\w+]* also matches your string is because [\.\w+] matches a . or a character from \w, which is then repeated zero or more time by the * after it. But, \w+[\.\w+]* will therefor also match strings like aaaaa or aaa............

(... ),正如我已经提到的,只是用于对字符进行分组(并且可能重复这些组)。

The (...) is, as I already mentioned, simply used to group characters (and possible repeat those groups).

有关字符集的更多信息:< a href =http://www.regular-expressions.info/charclass.html =noreferrer> http://www.regular-expressions.info/charclass.html

More info on character sets: http://www.regular-expressions.info/charclass.html

有关群组的更多信息: http://www.regular- expressions.info/brackets.html

More info on groups: http://www.regular-expressions.info/brackets.html

这是Java中的一个例子(见你主要发表Java答案):

Here's an example in Java (seeing you post mostly Java answers):

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String text = "some.text.here only but not Some other " + 
                "there some.name.separated.by.dots and.we are done!";
        Pattern p = Pattern.compile("\\w+(\\.\\w+)+");
        Matcher m = p.matcher(text);
        while(m.find()) {
            System.out.println(m.group());
        }
    }
}

将产生:

some.text.here
some.name.separated.by.dots
and.we

请注意 m.group(0) m.group()是等价的:意思是整场比赛。

Note that m.group(0) and m.group() are equivalent: meaning "the entire match".

这篇关于如何匹配重复模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆