如何在Java中的String.contains()方法中使用正则表达式

我想检查一个字符串是否按顺序包含单词“ stores”,“ store”和“ product”。无论它们之间是什么。


我尝试使用someString.contains(stores%store%product);并且.contains("stores%store%product");


我是否需要显式声明一个正则表达式并将其传递给方法,还是根本无法传递正则表达式?


慕码人8056858
浏览 7566回答 3
3回答

潇潇雨雨

public static void main(String[] args) {&nbsp; &nbsp; String test = "something hear - to - find some to or tows";&nbsp; &nbsp; System.out.println("1.result: " + contains("- to -( \\w+) som", test, null));&nbsp; &nbsp; System.out.println("2.result: " + contains("- to -( \\w+) som", test, 5));}static boolean contains(String pattern, String text, Integer fromIndex){&nbsp; &nbsp; if(fromIndex != null && fromIndex < text.length())&nbsp; &nbsp; &nbsp; &nbsp; return Pattern.compile(pattern).matcher(text).find();&nbsp; &nbsp; return Pattern.compile(pattern).matcher(text).find();}1.结果:正确2.结果:正确
打开App,查看更多内容
随时随地看视频慕课网APP