Java regular expression word match

前端 未结 5 653
孤街浪徒
孤街浪徒 2021-01-25 15:05

I have 3 values IU, PRI and RET. if my input string contains any one or more value(s),
the Java regular expression should return true.

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 15:54

    I don't know if you are still looking for the solution of this. But here's the code for your question. I assumed that the anagrams you are looking for are separated by spaces and the words appear in Uppercase.

        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());
    

    If you are try for case insensitive matching, change the pattern to the following;

        (?i)([UI]{2}|[PRI]{3}|[RET]{3})
    

提交回复
热议问题