Java regular expressions, find letters from which to form a word

Question:

There is a line in which you need to find letters from which you can make a certain word, i.e. each letter must appear at least once.

For example: String s=" oasd c ";

Regular expression to it `

    Pattern pattern = Pattern.compile("c+ & o+");



    Matcher matcher = pattern.matcher(toReturn);


    return matcher.find();`

Doesn't work, should return true but return false How to write an expression that will check a string where each letter that will be specified occurs one or more times. I tried it in different ways, it took an hour.

Answer:

Positive Lookahead can be used. For example, for the expression

(?=.*м)(?=.*а)(?=.*р)(?=.*т)(.*)

and test suite

мама мыра раму
мама мыра ртаму
трам
тран

lines will be matched

мама мыра ртаму
трам
Scroll to Top