Java Regex如何查找String是否包含字符,但顺序不是问题 [英] Java Regex How to Find if String Contains Characters but order is not a matter

查看:163
本文介绍了Java Regex如何查找String是否包含字符,但顺序不是问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串abcdefgh

我想检查字符串是否包含以下字符 [fcb]

I want to check the string contains the following characters [fcb]

条件是:字符串必须包含 任何顺序中的所有字符。

Condition is : The string must contain all characters in any order.

如何为此写一个正则表达式。

How to write a regex for this one.

我试过了以下正则表达式:

I tried following regexes :

。* [fcb]。* --->在这种情况下,它不会检查所有字符。如果任何一个字符匹配,它将返回true

.*[fcb].* ---> In this case it not check all characters. If any one character matchs it will return true

推荐答案

不要使用正则表达式。只需使用 String.contains 依次测试每个字符:

Don't use regex. Just use String.contains to test for each of the characters in turn:

in.contains("f") && in.contains("c") && in.contains("b")

这篇关于Java Regex如何查找String是否包含字符,但顺序不是问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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