Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用空格检查子字符串_Java_Regex_String - Fatal编程技术网

Java 用空格检查子字符串

Java 用空格检查子字符串,java,regex,string,Java,Regex,String,我试图从字符串中提取一些带有空格的数据。这是我的密码: if (somestring.contains("LOOP with spaces")) { i++; } 是否有一个正则表达式来提取这个:我尝试了这个,但没有成功 somestring.contains("LOOP\swith\s spaces")) 如果要将正则表达式用作参数,请改用匹配: if(somestring.matches(“.*LOOP\\swith\\ssspaces.*”){ 请注意,(1)*表示任意数

我试图从字符串中提取一些带有空格的数据。这是我的密码:

if (somestring.contains("LOOP with spaces")) {
    i++;
}   
是否有一个正则表达式来提取这个:我尝试了这个,但没有成功

somestring.contains("LOOP\swith\s spaces"))

如果要将正则表达式用作参数,请改用
匹配

if(somestring.matches(“.*LOOP\\swith\\ssspaces.*”){


请注意,(1)
*
表示任意数量的字符,(2)您需要在Java中转义反斜杠:
\\
被解释为
\

为什么要使用正则表达式?仅查找字符串有什么问题吗?不返回任何内容(“带空格的循环”)我在Java中就是这样做的。您正在测试的字符串是什么?@Bathsheba可能是OP有多个空间,并且想要类似于
*LOOP\\s+和\\s+空格的东西。*
。很难说,因为问题没有明确说明。我使用的字符串是“INFO-LOOP with spaces:这里有其他文本在任何一行中”@用户3588388那么您想做什么?检查字符串是否包含
带空格的循环
?或者提取
带空格的循环
-提取常量字符串的点是什么?因为“循环”和“循环”可能不相同?