java - myString.replaceAll(regex ,"") 未按预期工作

标签 java regex eclipse string replaceall

正如我在这里被问到的那样Deleting all regex instances starting with char '[' and ending with char ']' from a String 。我想删除 String 中任何开始 [ 和结束 ] 之间的所有 regex。 p>

我使用的答案是myString = myString .replaceAll("\\[([\\w\\%\\@]+)\\]","");。由@cuong hoang 提供。现在,该方法对于大多数 Srtring 都运行良好,但它在使用此 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!";

使用方法 myString = myString.replaceAll("\\[([\\w\\%\\@]+)\\]",""); 得到的输出 上面的StringSAME String 完全相同。

也许当您得到[blablabla]时,使用该regex的方法不能正常工作,之后我得到另一个[blabla2]这里??

最佳答案

您可以使用此 replaceAll 来代替否定字符类:

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

正则表达式分解:

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

关于java - myString.replaceAll(regex ,"") 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205033/

相关文章:

java - 在 Swing GUI 中选择继承还是组合

java - Gson处理多个json根元素

JavaScript 正则表达式 : ignore every letter after backslash

java - 什么时候在 Eclipse 中创建多个项目?

android - 依赖=[com.google.android.gms :google-play-services:apklib:7:compile] not found in workspace

java - 我在方法上苦苦挣扎……尤其是你通过它的论点。

java - 如何在Web环境下禁用log4j?

javascript - 如何查找以 # 开头的行,但有异常(exception)

mysql - 替换mysql表列中的部分字符串

java - eclipse 错误?私有(private)字段的 Java/Groovy 可见性