myString.replaceAll(regex,“”) not working as expected

六眼飞鱼酱① 提交于 2019-12-25 07:02:25

问题


As I've been asked here Deleting all regex instances starting with char '[' and ending with char ']' from a String. I want to delete all the regex's between any opening [ and closing ] in a String.

The answer i used is myString = myString .replaceAll("\\[([\\w\\%\\@]+)\\]","");. provided by @cuong hoang. Now the method is working well with most of the Srtring's but It's making some problems with this 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!";

The output I get from using the method myString = myString.replaceAll("\\[([\\w\\%\\@]+)\\]",""); on the String above is the SAME String exactly.

Maybe the method with that regex is not working well when you got [blablabla] and after that another [blabla2] which i got here??


回答1:


You can use this replaceAll instead with negated character class:

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

Regex Breakup:

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


来源:https://stackoverflow.com/questions/36205033/mystring-replaceallregex-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!