正则表达式在Java中查找特殊字符

时间:2021-10-22 00:25:47

I can use

我可以用

var regex = /[$&+,:;=?@#|]/;

if(elem.match(regex)) { // do something 
}

to find whether there is any special characters in string in Javascript.

查找Javascript中的字符串中是否有任何特殊字符。

How can I use the similar regular expression in Java?

如何在Java中使用类似的正则表达式?

I have tried:

我努力了:

str.match("\\="); // return false

For example:

例如:

String str = "123=456";

I tried to detect "=" in str.

我试图在str中检测到“=”。

That did not work. What am I doing wrong?

那没用。我究竟做错了什么?

4 个解决方案

#1


15  

Try this.

尝试这个。

Pattern regex = Pattern.compile("[$&+,:;=?@#|]");
Matcher matcher = regex.matcher("123=456");
if (matcher.find()){
    // Do something
}

EDIT: matches() checks all the string and find() finds it in any part of the string.

编辑:matches()检查所有字符串,find()在字符串的任何部分找到它。

A link: http://docs.oracle.com/javase/tutorial/essential/regex/index.html

链接:http://docs.oracle.com/javase/tutorial/essential/regex/index.html

#2


2  

Use String.matches(). Read the Javadocs for supported syntax.

使用String.matches()。阅读Javadocs以获取支持的语法。

#3


0  

Use

使用

String.matches(String regex);

#4


-3  

Here's some sample code:

这是一些示例代码:

import java.lang.Scanner;
/*setup everything here, you just need that import*/
Scanner input = new Scanner(System.in);
//ask for input
String str = input.next();
if(str.contains(/*char1*/) || str.contains(/*char2*/) || /*continue the pattern, 1 for every character*/)
{
    //event if it contains one
}
else
{
    //event if it does not contain any
}

an alternatve:

一个替代方案:

import java.lang.Scanner;
/*put the program parts here*/
Scanner input = new Scanner(System.in);
String str = input.next();
List<char> chars = Arrays.asList(/*put chars here, seperated with commas*/);
for(int x = 0; x < chars.size();x++)
{
    if(str.contains(chars[x]))
    {
        //event if it contains one
    }
    else
    {
        //event if it does not contain one
    }
}
//resume program

these code snippets will work, assuming everything is imported correctly.

假设所有内容都正确导入,这些代码段将起作用。

#1


15  

Try this.

尝试这个。

Pattern regex = Pattern.compile("[$&+,:;=?@#|]");
Matcher matcher = regex.matcher("123=456");
if (matcher.find()){
    // Do something
}

EDIT: matches() checks all the string and find() finds it in any part of the string.

编辑:matches()检查所有字符串,find()在字符串的任何部分找到它。

A link: http://docs.oracle.com/javase/tutorial/essential/regex/index.html

链接:http://docs.oracle.com/javase/tutorial/essential/regex/index.html

#2


2  

Use String.matches(). Read the Javadocs for supported syntax.

使用String.matches()。阅读Javadocs以获取支持的语法。

#3


0  

Use

使用

String.matches(String regex);

#4


-3  

Here's some sample code:

这是一些示例代码:

import java.lang.Scanner;
/*setup everything here, you just need that import*/
Scanner input = new Scanner(System.in);
//ask for input
String str = input.next();
if(str.contains(/*char1*/) || str.contains(/*char2*/) || /*continue the pattern, 1 for every character*/)
{
    //event if it contains one
}
else
{
    //event if it does not contain any
}

an alternatve:

一个替代方案:

import java.lang.Scanner;
/*put the program parts here*/
Scanner input = new Scanner(System.in);
String str = input.next();
List<char> chars = Arrays.asList(/*put chars here, seperated with commas*/);
for(int x = 0; x < chars.size();x++)
{
    if(str.contains(chars[x]))
    {
        //event if it contains one
    }
    else
    {
        //event if it does not contain one
    }
}
//resume program

these code snippets will work, assuming everything is imported correctly.

假设所有内容都正确导入,这些代码段将起作用。