簡體   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