删除所有正则表达式的情况下,开始用char'['和用char结束']'从字符串 [英] Deleting all regex instances starting with char '[' and ending with char ']' from a String
问题描述
我需要一个字符串
键,删除所有正则表达式在它开始与字符
'['和结束与字符
']'。
I need to take a String
and deleting all the regexes in it starting with character
'[' and ending with character
']'.
现在我不知道如何解决这个问题。我试图字符串
字符数组转换,然后从任起点'[',直到他结束']',然后将其转换回<$ C $把空字符C>字符串使用的toString()
方法。
Now i don't know how to tackle this problem. I tried to convert the String
to character array and then putting empty characters from any starting '[' till his closing ']' and then convert it back to a String
using toString()
method.
我的code:
char[] lyricsArray = lyricsParagraphElements.get(1).text().toCharArray();
for (int i = 0;i < lyricsArray.length;i++)
{
if (lyricsArray[i] == '[')
{
lyricsArray[i] = ' ';
for (int j = i + 1;j < lyricsArray.length;j++)
{
if (lyricsArray[j] == ']')
{
lyricsArray[j] = ' ';
i = j + 1;
break;
}
lyricsArray[j] = ' ';
}
}
}
String songLyrics = lyricsArray.toString();
System.out.println(songLyrics);
但在songLyrics的打印行,我得到怪异的东西,像
But in the print line of songLyrics i get weird stuff like
[C@71bc1ae4
[C@6ed3ef1
[C@2437c6dc
[C@1f89ab83
[C@e73f9ac
[C@61064425
[C@7b1d7fff
[C@299a06ac
[C@383534aa
[C@6bc168e5
我想有它的简单方法。任何帮助将是非常美联社preciated。
I guess there is a simple method for it. Any help will be very appreciated.
有关澄清:
转换ABCD [dsadsadsa] EFG [ADF%@ 1] D
进入abcdefgd
推荐答案
或者干脆使用普通的前pression到的替换 \\\\所有
OCCURENCES [* \\\\] 一无所有:
Or simply use a regular expression to replace all occurences of \\[.*\\]
with nothing:
String songLyrics = text.replaceAll("\\[.*?\\]", "");
其中,文本
是ofcourse:
String text = lyricsParagraphElements.get(1).text();
这是什么 \\\\ [* \\\\]
意思?
What does \\[.*\\]
mean?
的replaceAll
的第一个参数是描述一个普通的前pression的字符串。定期EX pression定义一个模式在字符串相匹配。
The first parameter of replaceAll
is a string describing a regular expression. A regular expression defines a pattern to match in a string.
因此,让我们把它分解了:
So let's split it up:
\\\\ [
完全匹配字符 [
。由于 [
有一个普通的前pression中的特殊意义,它需要进行转义(两次!)。
\\[
matches exactly the character [
. Since [
has a special meaning within a regular expression, it needs to be escaped (twice!).
。
匹配任何字符,用(<一本结合起来href=\"http://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-ex$p$pssions\">lazy)零或更多的运营商 *
,并且直到最后发现它会匹配任何字符:
.
matches any character, combine this with the (lazy) zero-or-more operator *?
, and it will match any character until it finally finds:
\\\\]
,其中字符匹配]
。注意再次逃脱。
\\]
, which matches the character ]
. Note the escaping again.
这篇关于删除所有正则表达式的情况下,开始用char'['和用char结束']'从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!