如何使用此正则表达式拆分String?

时间:2020-12-11 21:38:10
if (url.contains("|##|")) {
    Log.e("url data", "" + url);
    final String s[] = url.split("\\|##|");
}

I have a URL with the separator "|##|"

我有一个带分隔符“| ## |”的URL

I tried to separate this but didn't find solution.

我试图分开这个,但没有找到解决方案。

3 个解决方案

#1


4  

Use Pattern.quote, it'll do the work for you:

使用Pattern.quote,它将为您完成工作:

Returns a literal pattern String for the specified String.

返回指定String的文字模式String。

final String s[] = url.split(Pattern.quote("|##|"));

Now "|##|" is treated as the string literal "|##|" and not the regex "|##|". The problem is that you're not escaping the second pipe, it has a special meaning in regex.

现在“| ## |”被视为字符串文字“| ## |”而不是正则表达式“| ## |”。问题是你没有逃避第二个管道,它在正则表达式中具有特殊含义。

An alternative solution (as suggested by @kocko), is escaping* the special characters manually:

另一种解决方案(由@kocko建议)可以手动转义*特殊字符:

final String s[] = url.split("\\|##\\|");

* Escaping a special character is done by \, but in Java \ is represented as \\

*转义特殊字符由\完成,但在Java \中表示为\\

#2


2  

You have to escape the second |, as it is a regex operator:

你必须逃避第二个|,因为它是一个正则表达式运算符:

final String s[] = url.split("\\|##\\|");

#3


1  

You should try to understand the concept as well - String.split(String regex) interprets the parameter as a regular expression, and since pipe character "|" is a logical OR in regular expression, you would be getting result as an array of each alphabet is your word.
Even if you had used url.split("|"); you would have got same result.

您应该尝试理解这个概念 - String.split(String regex)将参数解释为正则表达式,并且因为管道符“|”是正则表达式中的逻辑OR,您将获得结果,因为每个字母的数组都是您的单词。即使你使用过url.split(“|”);你会得到相同的结果。

Now why the String.contains(CharSequence s) passed the |##| in the start because it interprets the parameter as CharSequence and not a regular expression.

现在为什么String.contains(CharSequence s)传递了| ## |在开始时因为它将参数解释为CharSequence而不是正则表达式。

Bottom line: Check the API that how the particular method interprets the passed input. Like we have seen, in case of split() it interprets as regular expression while in case of contains() it interprets as character sequence.

结论:检查API,了解特定方法如何解释传递的输入。就像我们所看到的,在split()的情况下,它解释为正则表达式,而在contains()的情况下,它解释为字符序列。

You can check the regular expression constructs over here - http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

你可以在这里查看正则表达式结构 - http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

#1


4  

Use Pattern.quote, it'll do the work for you:

使用Pattern.quote,它将为您完成工作:

Returns a literal pattern String for the specified String.

返回指定String的文字模式String。

final String s[] = url.split(Pattern.quote("|##|"));

Now "|##|" is treated as the string literal "|##|" and not the regex "|##|". The problem is that you're not escaping the second pipe, it has a special meaning in regex.

现在“| ## |”被视为字符串文字“| ## |”而不是正则表达式“| ## |”。问题是你没有逃避第二个管道,它在正则表达式中具有特殊含义。

An alternative solution (as suggested by @kocko), is escaping* the special characters manually:

另一种解决方案(由@kocko建议)可以手动转义*特殊字符:

final String s[] = url.split("\\|##\\|");

* Escaping a special character is done by \, but in Java \ is represented as \\

*转义特殊字符由\完成,但在Java \中表示为\\

#2


2  

You have to escape the second |, as it is a regex operator:

你必须逃避第二个|,因为它是一个正则表达式运算符:

final String s[] = url.split("\\|##\\|");

#3


1  

You should try to understand the concept as well - String.split(String regex) interprets the parameter as a regular expression, and since pipe character "|" is a logical OR in regular expression, you would be getting result as an array of each alphabet is your word.
Even if you had used url.split("|"); you would have got same result.

您应该尝试理解这个概念 - String.split(String regex)将参数解释为正则表达式,并且因为管道符“|”是正则表达式中的逻辑OR,您将获得结果,因为每个字母的数组都是您的单词。即使你使用过url.split(“|”);你会得到相同的结果。

Now why the String.contains(CharSequence s) passed the |##| in the start because it interprets the parameter as CharSequence and not a regular expression.

现在为什么String.contains(CharSequence s)传递了| ## |在开始时因为它将参数解释为CharSequence而不是正则表达式。

Bottom line: Check the API that how the particular method interprets the passed input. Like we have seen, in case of split() it interprets as regular expression while in case of contains() it interprets as character sequence.

结论:检查API,了解特定方法如何解释传递的输入。就像我们所看到的,在split()的情况下,它解释为正则表达式,而在contains()的情况下,它解释为字符序列。

You can check the regular expression constructs over here - http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

你可以在这里查看正则表达式结构 - http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html