2

我有 3 个值IU、PRIRET。如果我的输入字符串包含任何一个或多个值,
Java 正则表达式应该返回 true。

Ex:
Values : IU PRI RET 
Input String : "put returns UI between paragraphs"

输入字符串包含“UI”字样,Java 正则表达式应返回 true。

4

5 回答 5

12

为此,您需要单词边界

boolean foundMatch = false;
Pattern regex = Pattern.compile("\\b(?:UI|PRI|RET)\\b");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
于 2013-06-18T09:53:28.710 回答
4

尝试

String s= "A IU somehting PRI something RET whatever";

Pattern p= Pattern.compile("(IU|PRI|RET)");
Matcher m= p.matcher(s);
while (m.find()) {
    String matched= m.group(1);
    System.out.println(matched);
}

它打印:

IU
PRI
RET
于 2013-06-18T09:55:21.173 回答
1

我不知道您是否仍在寻找解决方案。但这是您问题的代码。我假设您要查找的字谜由空格分隔,并且单词以大写字母显示。

    String text = "put returns UI between IU paragraphs PRI RIP and RET ETR";
    Pattern p = Pattern.compile("([UI]{2}|[PRI]{3}|[RET]{3})");

    Matcher m = p.matcher(text);
    System.out.println(m.find());

如果您尝试不区分大小写的匹配,请将模式更改为以下内容;

    (?i)([UI]{2}|[PRI]{3}|[RET]{3})
于 2013-06-24T04:11:13.780 回答
0

好的,这是一个疯狂的解决方案,每个给定的字谜String,内置于一个Pattern只是为了好玩:

public static void main(String[] args) {
    try {
        Pattern pattern = makePattern("IU", "PRI", "RET");
        System.out.println(pattern.pattern());
        String test = "put returns UI between paragraphs, also IRP and TER";
        Matcher matcher = pattern.matcher(test);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
public static Pattern makePattern(String... words) throws Exception {
    if (words == null || words.length == 0) {
        throw new Exception("TODO handle invalid argument");
    }
    StringBuilder patternBuilder = new StringBuilder("(");
    for (String word : words) {
        if (word == null || word.isEmpty()) {
            throw new Exception("TODO invalid word");
        }
        for (String anagram: doAnagrams(word, null)) {
            patternBuilder.append("\\b").append(anagram).append("\\b").append("|");
        }
    }
    patternBuilder.deleteCharAt(patternBuilder.length() - 1);
    patternBuilder.append(")");
    return Pattern.compile(patternBuilder.toString());
}
public static Set<String> doAnagrams(String original, Set<String> processed) {
    if (original == null || original.isEmpty()) {
        return new LinkedHashSet<String>();
    }
    Set<String> result;
    if (processed == null) {
        result = new LinkedHashSet<String>();
        result.add(original);
    } else {
        result = processed;
    }
    if (original.length() <= 1) {
        return result;
    }
    String sub = original.substring(1);
    String subStart = original.substring(0, 1);
    for (String subAnagram : doAnagrams(sub, null)) {
        result.add(subAnagram.concat(subStart));
    }
    if (sub.concat(original.substring(0, 1)).equals(result.iterator().next())) {
        return result;
    } 
    else {
        return doAnagrams(sub.concat(subStart), result);
    }
}

输出

(\bIU\b|\bUI\b|\bPRI\b|\bRIP\b|\bIRP\b|\bIPR\b|\bPIR\b|\bRPI\b|\bRET\b|\bETR\b|\bTER\b|\bTRE\b|\bRTE\b|\bERT\b)
UI
IRP
TER
于 2013-06-18T11:24:54.823 回答
0

您可以在一行中执行此操作并获得您的布尔值。

boolean matcher = Pattern.matches("[UI]{2}|[PRI]{3}|[RET]{3}", stringToBeMatched);

于 2016-07-28T16:15:01.607 回答