Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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 使用lookahead匹配以引号开头和结尾的字符串,并去掉引号?_Java_Regex - Fatal编程技术网

Java 使用lookahead匹配以引号开头和结尾的字符串,并去掉引号?

Java 使用lookahead匹配以引号开头和结尾的字符串,并去掉引号?,java,regex,Java,Regex,我希望匹配以相同引号开头和结尾的字符串,但仅匹配开头和结尾的引号: "foo bar" 'foo bar' "the quick brown fox" 但我不想让这些衣服搭配或脱光: foo "bar" foo 'bar' 'foo bar" "the lazy" dogs 我尝试使用这个java regexp,但它并不适用于所有情况: Pattern.compile("^\"|^'(.+)\"$|'$").matcher(quotedString).replaceAll(""); 我认为

我希望匹配以相同引号开头和结尾的字符串,但仅匹配开头和结尾的引号:

"foo bar"
'foo bar'
"the quick brown fox"
但我不想让这些衣服搭配或脱光:

foo "bar"
foo 'bar'
'foo bar"
"the lazy" dogs
我尝试使用这个java regexp,但它并不适用于所有情况:

Pattern.compile("^\"|^'(.+)\"$|'$").matcher(quotedString).replaceAll("");
我认为有一种方法可以进行前瞻,但我不知道如何在这种情况下使用它

或者,设置一个单独检查它们的if语句会更有效吗

Pattern startPattern = Pattern.compile("^\"|^');
Pattern endPattern = Pattern.compile(\"$|'$");

if (startPattern.matcher(s).find() && endPattern.matcher(s).find()) {
    ...
}

(当然,这将匹配
“foo bar”
,这是我不想要的)

这里有一个方法可以检查您的所有需求:

public static boolean matches (final String str)
{
    boolean matches = false;

    // check for null string
    if (str == null) return false;

    if ((str.startsWith("\"") && str.endsWith("\"")) || 
        (str.startsWith("\'") && str.endsWith("\'")))
    {
        String strip = str.substring(1, str.length() - 1);

        // make sure the stripped string does not have a quote
        if (strip.indexOf("\"") == -1 && strip.indexOf("\'") == -1)
        {
            matches = true;
        }
    }
    return matches;
}
测试

public static void main(String[] args)
{
    System.out.println("Should Pass\n-----------");
    System.out.println(matches("\"foo bar\""));
    System.out.println(matches("\'foo bar\'"));
    System.out.println(matches("\"the quick brown fox\""));

    System.out.println("\nShould Fail\n-----------");
    System.out.println(matches("foo \"bar\""));
    System.out.println(matches("foo \'bar\'"));
    System.out.println(matches("'foo bar\""));
    System.out.println(matches("\"the lazy\" dogs"));

}
Should Pass
-----------
true
true
true

Should Fail
-----------
false
false
false
false
输出

public static void main(String[] args)
{
    System.out.println("Should Pass\n-----------");
    System.out.println(matches("\"foo bar\""));
    System.out.println(matches("\'foo bar\'"));
    System.out.println(matches("\"the quick brown fox\""));

    System.out.println("\nShould Fail\n-----------");
    System.out.println(matches("foo \"bar\""));
    System.out.println(matches("foo \'bar\'"));
    System.out.println(matches("'foo bar\""));
    System.out.println(matches("\"the lazy\" dogs"));

}
Should Pass
-----------
true
true
true

Should Fail
-----------
false
false
false
false

这里有一种方法可以检查您的所有需求:

public static boolean matches (final String str)
{
    boolean matches = false;

    // check for null string
    if (str == null) return false;

    if ((str.startsWith("\"") && str.endsWith("\"")) || 
        (str.startsWith("\'") && str.endsWith("\'")))
    {
        String strip = str.substring(1, str.length() - 1);

        // make sure the stripped string does not have a quote
        if (strip.indexOf("\"") == -1 && strip.indexOf("\'") == -1)
        {
            matches = true;
        }
    }
    return matches;
}
测试

public static void main(String[] args)
{
    System.out.println("Should Pass\n-----------");
    System.out.println(matches("\"foo bar\""));
    System.out.println(matches("\'foo bar\'"));
    System.out.println(matches("\"the quick brown fox\""));

    System.out.println("\nShould Fail\n-----------");
    System.out.println(matches("foo \"bar\""));
    System.out.println(matches("foo \'bar\'"));
    System.out.println(matches("'foo bar\""));
    System.out.println(matches("\"the lazy\" dogs"));

}
Should Pass
-----------
true
true
true

Should Fail
-----------
false
false
false
false
输出

public static void main(String[] args)
{
    System.out.println("Should Pass\n-----------");
    System.out.println(matches("\"foo bar\""));
    System.out.println(matches("\'foo bar\'"));
    System.out.println(matches("\"the quick brown fox\""));

    System.out.println("\nShould Fail\n-----------");
    System.out.println(matches("foo \"bar\""));
    System.out.println(matches("foo \'bar\'"));
    System.out.println(matches("'foo bar\""));
    System.out.println(matches("\"the lazy\" dogs"));

}
Should Pass
-----------
true
true
true

Should Fail
-----------
false
false
false
false

你要找的正则表达式是

^(["'])(.*)\1$
替换字符串为
“$2”


演示:

您要查找的正则表达式是

^(["'])(.*)\1$
替换字符串为
“$2”


DEMO:

你是说当他们包围两个或多个单词时,你想删去单引号或双引号,在引号中用空格隔开?如果字符串中间有引号怎么办?(例如‘这是我的棕色狐狸’)@ MichaelMarkidis -我不担心那个角落的情况,这与命令行的东西有关,所以无论如何都不是有效的。你是说当他们围绕两个或多个单词,在引号中用空格隔开时,你想删去单引号或双引号吗?如果字符串中间有引号怎么办?(例如‘这是我的棕色狐狸’)@MichaelMarkidis-我不担心那个角落的案子,这与命令行有关,所以那无论如何都是无效的。