2014-03-14 155 views
2

我想從String中刪除字符序列,如"htsap://""ftsap://"。可能嗎?
讓我用一個例子來說明我的需求。刪除與正則表達式匹配的所有單詞

實際輸入字符串:

"Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For "ftsap://"example, the URL for CSM Library's home page is: "htsap://"www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to htsap://where a web page originates and who might be responsible for the information at that page or site." 

預期的結果String:

"Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For example, the URL for CSM Library's home page is: www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to where a web page originates and who might be responsible for the information at that page or site." 

模式我想:(還不是很確定這是一個正確的方式)

((.*?)(?=("htsap://|ftsap://"))) 

和:

((.*?)(?=("htsap://|ftsap://")))(.*) 

任何人都可以在這裏建議嗎?

回答

1

由於您在示例String中轉義引號,因此我假設您正在使用Java。

你應該嘗試:

final String res = input.replaceAll("\"?\\w+://\"?", ""); 

Here is a link到的究竟是什麼這個正則表達式匹配工作的例子!


工作原理:

它匹配並移除的字母數字字符的任何序列(下劃線),隨後://和可能由前面"和/或之後。


編輯:如何使用Matcher實現相同的結果?

final String input = "Every Web page has a http unique address called a URL (Uniform Resource Locator) which identifies where it is located on the Web. For \"ftsap://\"example, the URL for CSM Library's home page is: \"htsap://\"www.smccd.edu/accounts/csmlibrary/index.htm The basic parts of a URL often provide \"clues\" to htsap://where a web page originates and who might be responsible for the information at that page or site."; 
final Pattern p = Pattern.compile("\"?\\w+://\"?"); 
final StringBuilder b = new StringBuilder(input); 
Matcher m; 
while((m = p.matcher(b.toString())).find()) { 
    b.replace(m.start(), m.end(), ""); 
} 

System.out.println(b.toString()); 
+0

正確,如果我錯了,但'g'lobal標誌應該用於'replaceAll'或等效方法未被使用。 –

+0

你是對的!其實,我在我的例子中使用了這個標誌(在我的答案中,永久鏈接** regex101 **)。 – ccjmne

+0

好吧,我現在看到。感謝澄清。 –

0

使用這個表達式:

"(ftsap|htsap).//" 

而且隨着''

正則表達式替換它解釋說:

"(ftsap|htsap).//" with flag g 

Regular expression visualization

Debuggex Demo

相關問題