繁体   English   中英

我如何在java中表示这个正则表达式?

[英]How can I represent this regex in java?

我有这个正则表达式: [^\\w\\s+]

Regex101链接。

我试图在 java 中使用它来替换匹配的字符。 我意识到我需要转义括号和斜线,但它仍然没有按预期工作。

这是我到目前为止:

content = content.replaceAll("\\[^\\w\\s+\\]", "");

简短版本:不要转义括号。

精彩的在线工具https://regex101.com/可帮助您正确运行正则表达式。 如果您插入表达式并使用代码生成器(左侧边栏),它会为 Java 生成以下内容(以及一些使用正则表达式的代码):

final String regex = "[^\\w\\s+]";

你应该像那样使用正则表达式

String text = "RegExr was created by[...], and is proudly hosted by Media Temple.Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.";
text=text.replaceAll("[^\\w\\s+]","");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM