2016-03-24 6 views
0

Wie ich hier gefragt wurde Deleting all regex instances starting with char '[' and ending with char ']' from a String. Ich möchte alle regex 's zwischen jeder Öffnung [ und Schließen ] in einem String löschen.myString.replaceAll (regex, "") funktioniert nicht wie erwartet

Die Antwort, die ich verwendete, ist myString = myString .replaceAll("\\[([\\w\\%\\@]+)\\]","");. bereitgestellt von @cuong hoang. Nun wird das Verfahren gut funktioniert mit den meisten der Srtring ‚s aber Es macht einige Probleme mit diesem String:

   "[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!"; 

Der Ausgang I aus der Verwendung des Verfahrens erhalten myString = myString.replaceAll("\\[([\\w\\%\\@]+)\\]",""); auf dem String oben ist die SAMEString genau.

Vielleicht funktioniert die Methode mit der regex nicht gut, wenn Sie [blablabla] bekommen und danach eine weitere [blabla2], die ich hier bekommen ??

+0

Versuchen: 'myString = myString.replaceAll (" \\ [[^ \\ ]] * \\] "," ");' – anubhava

+0

@anubhava Du meinst 'myString = myString.replaceAll (" \\ [[^ \\]] * \\] "," ");'? – God

+1

@anubhava Ja, das funktioniert. Kannst du es als Antwort posten und ein wenig über die Methodenfunktion erklären? Vielen Dank. – God

Antwort

4

können Sie verwenden diese replaceAll stattdessen mit negierte Zeichenklasse:

myString = myString.replaceAll("\\[[^\\]]*\\]", ""); 

Regex Breakup:

\\[  # match a literal [ 
[^\\]]* # match 0 or more of any character that is not ] 
\\]  # match a literal ] 
+1

Danke Mann. :). – God

Verwandte Themen