Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我想用空格字符或%20字符串分割一个字符串,我应该如何编写我的正则表达式?
%20
我尝试了以下方法,但没有奏效。
String regex = "[\\s+, %20]"; String str1 = "abc%20xyz"; String str2 = "abc xyz"; str1.split(regex); str2.split(regex);
正则表达式似乎不适用于str1.
str1
使用交替|:
|
String regex = "(?:\\s+|%20)+";
String regex = "(\\s{1}+|%20{1}+)";
如果你想用一个空格或一个“%20”分割,试试这个:
字符串正则表达式 = "(\\s|%20)";
如果您想按至少一个空格或至少一个“%20”进行拆分,请尝试以下操作:
字符串正则表达式 = "(\\s+|(%20)+)";