Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 - Fatal编程技术网

java中的正则表达式

java中的正则表达式,java,regex,Java,Regex,我想通过正则表达式在字符串中搜索“$\u POST['something']”。 用下面的正则表达式通过textpad进行了尝试>“*$\u POST\['[a-zA-z0-9]*'\].*” 当java中使用同样的方法时,它现在可以工作了 使用以下命令: "\\$_POST\\['([a-zA-Z0-9]*)'\\]" 像$这样的符号在正则表达式中有特殊的含义。因此,您需要在它们前面加上前缀\使用以下方法: "\\$_POST\\['([a-zA-Z0-9]*)'\\]" 像$这样的符号在

我想通过正则表达式在字符串中搜索“$\u POST['something']”。 用下面的正则表达式通过textpad进行了尝试<代码>>“*$\u POST\['[a-zA-z0-9]*'\].*”

当java中使用同样的方法时,它现在可以工作了

使用以下命令:

"\\$_POST\\['([a-zA-Z0-9]*)'\\]"
$
这样的符号在正则表达式中有特殊的含义。因此,您需要在它们前面加上前缀
\

使用以下方法:

"\\$_POST\\['([a-zA-Z0-9]*)'\\]"

$
这样的符号在正则表达式中有特殊的含义。因此,您需要在它们前面加上
\

前缀。您不需要开头和结尾的
*
,但需要转义
$
,因为它具有字符串零宽度结尾的特殊含义

\\$_POST\\['[a-zA-Z0-9]*'\\]

您不需要开头和结尾的
*
,但需要转义
$
,因为它具有字符串零宽度结尾的特殊含义

\\$_POST\\['[a-zA-Z0-9]*'\\]

您可以使用以字符串形式给出的正则表达式模式。它返回一个
布尔值

String a = "Hello, world! $_POST['something'] Test!";
String b = "Hello, world! $_POST['special!!!char'] Test!";
String c = "Hey there $_GET['something'] foo bar";

String pattern = ".*\\$_POST\\['[A-Za-z0-9]+'\\].*";

System.out.println ("a matches? " + Boolean.toString(a.matches(pattern)));
System.out.println ("b matches? " + Boolean.toString(b.matches(pattern)));
System.out.println ("c matches? " + Boolean.toString(c.matches(pattern)));
您还可以使用和Matcher对象将模式重新用于多种用途:

String[] array = {
    "Hello, world! $_POST['something'] Test!",
    "Hello, world! $_POST['special!!!char'] Test!",
    "Hey there $_GET['something'] foo bar"
};

String strPattern = ".*\\$_POST\\['[A-Za-z0-9]+'\\].*";
Pattern p = Pattern.compile(strPattern);

for (int i=0; i<array.length; i++) {
    Matcher m = p.matcher(array[i]);
    System.out.println("Expression:  " + array[i]);
    System.out.println("-> Matches?  " + Boolean.toString(m.matches()));
    System.out.println("");
}

您可以使用以字符串形式给出的正则表达式模式。它返回一个
布尔值

String a = "Hello, world! $_POST['something'] Test!";
String b = "Hello, world! $_POST['special!!!char'] Test!";
String c = "Hey there $_GET['something'] foo bar";

String pattern = ".*\\$_POST\\['[A-Za-z0-9]+'\\].*";

System.out.println ("a matches? " + Boolean.toString(a.matches(pattern)));
System.out.println ("b matches? " + Boolean.toString(b.matches(pattern)));
System.out.println ("c matches? " + Boolean.toString(c.matches(pattern)));
您还可以使用和Matcher对象将模式重新用于多种用途:

String[] array = {
    "Hello, world! $_POST['something'] Test!",
    "Hello, world! $_POST['special!!!char'] Test!",
    "Hey there $_GET['something'] foo bar"
};

String strPattern = ".*\\$_POST\\['[A-Za-z0-9]+'\\].*";
Pattern p = Pattern.compile(strPattern);

for (int i=0; i<array.length; i++) {
    Matcher m = p.matcher(array[i]);
    System.out.println("Expression:  " + array[i]);
    System.out.println("-> Matches?  " + Boolean.toString(m.matches()));
    System.out.println("");
}

您可以在许多站点在线测试Java正则表达式,包括您可以在许多站点在线测试Java正则表达式,包括