Java myString.replaceAll(regex,";)未按预期工作
正如我在这里被问到的。我想删除Java myString.replaceAll(regex,";)未按预期工作,java,regex,eclipse,string,replaceall,Java,Regex,Eclipse,String,Replaceall,正如我在这里被问到的。我想删除字符串中任何开始[和结束]之间的所有正则表达式 我使用的答案是myString=myString.replaceAll(“\\[([\\w\\\%\\@]+)\\]”,”)。由@cuong hoang提供。现在,该方法可以很好地处理大多数Srtring,但是该字符串出现了一些问题: "[Intro:]\r\n" + "[Tuning Tv]\r\n" + "[Emin
字符串中任何开始[
和结束]
之间的所有正则表达式
我使用的答案是myString=myString.replaceAll(“\\[([\\w\\\%\\@]+)\\]”,”)代码>。由@cuong hoang提供。现在,该方法可以很好地处理大多数Srtring
,但是该字符串出现了一些问题:
"[Intro:]\r\n" +
"[Tuning Tv]\r\n" +
"[Eminem Snortin Crack]\r\n" +
"\r\n" +
"[Tv Presenter:]\r\n" +
"Hello boys and girls\r\n" +
"Today we're gonna talk about father and daughter relationships\r\n" +
"Do you have a daddy?\r\n" +
"I'll bet you do\r\n" +
"[Door opens]\r\n" +
"who's your daddy?\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"Daddy, what're you doing?\r\n" +
"\r\n" +
"Beat starts\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"Haha\r\n" +
"\r\n" +
"[Eminem & Hailie:]\r\n" +
"Ok then! everybody, listen up!\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"I'm goin to hell, who's comin' with me?\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"Somebody, please help him!\r\n" +
"[giggle]\r\n" +
"i think my dad gone crazy!\r\n" +
"\r\n" +
"[Verse #1:]\r\n" +
"There's no mountain i can't climb\r\n" +
"There's no tower too high,\r\n" +
"No plane that i can't learn how to fly\r\n" +
"What do i gotta do to get through to you, destroy you\r\n" +
"There ain't nothing i can't take this chainsaw to\r\n" +
"[Hailey Makes Chainsaw Sound]\r\n" +
"Fuckin' brain's brawn, and brass balls\r\n" +
"I cut 'em off, i got 'em pickled and bronzed in a glass jar\r\n" +
"Inside of a hall, with my framed autograph,\r\n" +
"Sunglasses with elton john's name, on my drag wall\r\n" +
"I'm out the closet, i been lying my ass off\r\n" +
"All this time, me and dre been fucking with hats off\r\n" +
"\r\n" +
"[Dr Dre:]\r\n" +
"Suck it marshall\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"Tell laura and her husband to back off\r\n" +
"Before i push this motherfucking button and blast off\r\n" +
"And launch one of these russians, and that's all\r\n" +
"[Hailie Makes Explosion Sound]\r\n" +
"Blow every fucking thing, except afghanistan on the map, off\r\n" +
"We wanna stop, we wanna knock the crap off\r\n" +
"[Knocking]\r\n" +
"Haley, tell 'em baby\r\n" +
"\r\n" +
"[Haley:]\r\n" +
"My dad's lost it\r\n" +
"\r\n" +
"[Chorus:]\r\n" +
"[Eminem:]\r\n" +
"There's really nothin' else to say ha, i can't explain it\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"I think my dad gone crazy!\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"A little help from hailie jade would, should tell them maybe\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"I think my dad gone crazy!\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"Theres nothing you could do to save it, could ever change me\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"I think my dad gone crazy!\r\n" +
"\r\n" +
"[Eminem:]\r\n" +
"There's noone on earth that can save me, not even halie\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"I think my dad gone crazy!\r\n" +
"\r\n" +
"[Verse 2:]\r\n" +
"It's like my mother always told me\r\n" +
"[Eminem Impersinates His Mom]\r\n" +
"Rnrnrnrnrnrrrr, n codeine n goddamit, you little motherfucker\r\n" +
"If you aint got nothin' nice to say then don't say nothin'\r\n" +
"Er..\r\n" +
"Fuck that shit, bitch, eat a motherfuckin' dick\r\n" +
"Chew on a prick, and lick a million motherfuckin' cocks for second\r\n" +
"I'd rather put out a motherfucking gospel record\r\n" +
"I'd rather be a pussy-whipped bitch, eat pussy\r\n" +
"And have pussy-lips glued to my face with a clit-ring in my nose\r\n" +
"Then quit bringin my flos, quit giving me my ammo\r\n" +
"Can't you see why i'm so mean? if y'all leave me alone, this wouldn't be my ammo\r\n" +
"I wouldn't have to go eenie meenie minie mo\r\n" +
"Catch a homo by his toe, man i don't know no more\r\n" +
"Am i the only fuckin one who's normal any more?\r\n" +
"\r\n" +
"[Hailey:]\r\n" +
"Dad\r\n" +
"\r\n" +
"[Chorus]\r\n" +
"\r\n" +
"[Verse #3:]\r\n" +
"My songs can make you cry, take you by surprise\r\n" +
"And at the same time, make you dry your eyes with the same rhyme\r\n" +
"So what you're seeing is a genius at work\r\n" +
"Which to me isn't work, so it's easy to misinterpret it at first,\r\n" +
"Cuz when i speak, it's tongue in cheek\r\n" +
"I'd gank my fuckin teeth before id ever bite my tongue\r\n" +
"I'd slice my gums, get struck by fuckin' lightning twice at once\r\n" +
"And die and come back as vanilla ice's son\r\n" +
"And walk around the rest of my life spit on\r\n" +
"And kicked and hit with shit, every time i sung\r\n" +
"Like r kelly as soon as \"bump n' grind\" comes on\r\n" +
"More pain inside o' my brain, in the eyes of a little girl inside of a plane\r\n" +
"Aimed at the world trade, standin' on ronny's grave,\r\n" +
"Screaming at the sky, the clouds gather as clyde mathers and bonnie jade\r\n" +
"And nash briddy musta just stop it\r\n" +
"Parents are pissed, but the kids love it\r\n" +
"Nine millimeter, heater's desk, and two-seaters with meat cleavers\r\n" +
"I don't blame you, i wouldn't let hailie listen to me neither\r\n" +
"\r\n" +
"[Chorus]\r\n" +
"\r\n" +
"[Outro:]\r\n" +
"[Eminem:]\r\n" +
"Crazy\r\n" +
"\r\n" +
"[Hailie:]\r\n" +
"[laughs]\r\n" +
"You're funny daddy!";
我使用方法myString=myString.replaceAll(“\\[([\\w\\%\\@]+)\\]”得到的输出上面字符串
上的code>与相同字符串
完全相同
当您得到[blabla]
时,可能使用该正则表达式的方法工作不好,然后我在这里得到了另一个[blablabla2]
。您可以使用该替换所有
字符类:
myString = myString.replaceAll("\\[[^\\]]*\\]", "");
正则表达式分解:
\\[ # match a literal [
[^\\]]* # match 0 or more of any character that is not ]
\\] # match a literal ]
您可以将此replaceAll
用于否定字符类:
myString = myString.replaceAll("\\[[^\\]]*\\]", "");
正则表达式分解:
\\[ # match a literal [
[^\\]]* # match 0 or more of any character that is not ]
\\] # match a literal ]
试试:myString=myString.replaceAll(“\\[^\\]]*\\\]”,“”)
@anubhava你的意思是myString=myString.replaceAll(“\\[[^\]]*\\]”,”)代码>?@anubhava是的,这很有效。你能把它作为一个答案贴出来,解释一下方法函数吗?谢谢。请尝试:myString=myString.replaceAll(“\\[^\]]*\\\]”,“”)
@anubhava你的意思是myString=myString.replaceAll(“\\[[^\]]*\\]”,”)代码>?@anubhava是的,这很有效。你能把它作为一个答案贴出来,解释一下方法函数吗?非常感谢。