2008-11-05 18 views

回答

4

這裏的另一種方法,使用一個超前來確定當前位置進來配對畢竟引號。

text = text.replaceAll(" ++(?=(?:[^\"]*+\"[^\"]*+\")*+[^\"]*+$)", " "); 

如果需要,可以調整lookahead以處理引用段內的轉義引號。

0

引號之間的文本:是在同一行還是多行內引號?

2

當試圖匹配的東西,可以包含別的東西中,它可以幫助構建一個同時匹配正則表達式,像這樣:

("[^"\\]*(?:\\.[^"\\]*)*")|( +) 

這將匹配帶引號的字符串或兩個以上空間。由於這兩個表達式組合在一起,它將匹配一個帶引號的字符串或兩個或多個空格,但引號內不包含空格。使用這個表情,你就需要檢查每場比賽以確定它是否是帶引號的字符串或兩個以上的空間和採取相應的行動:

Pattern spaceOrStringRegex = Pattern.compile("(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|( +)"); 

StringBuffer replacementBuffer = new StringBuffer(); 

Matcher spaceOrStringMatcher = spaceOrStringRegex.matcher(text); 

while (spaceOrStringMatcher.find()) 
{ 
    // if the space group is the match 
    if (spaceOrStringMatcher.group(2) != null) 
    { 
     // replace with a single space 
     spaceOrStringMatcher.appendReplacement(replacementBuffer, " "); 
    } 
} 

spaceOrStringMatcher.appendTail(replacementBuffer); 
0

記號化,併發出令牌之間一個空格。快速谷歌爲「Java的標記生成器,處理引號」翻起來: this link

因人而異

編輯:所以沒有這樣的鏈接。以下是谷歌搜索鏈接:google。這是第一個結果。

0

就個人而言,我不使用Java,但是這正則表達式可以做的伎倆:

([^\" ])*(\\\".*?\\\")* 

試圖與使用RegexBuddy的表達,它生成此代碼,看起來好像沒什麼問題:

try { 
    Pattern regex = Pattern.compile("([^\" ])*(\\\".*?\\\")*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); 
    Matcher regexMatcher = regex.matcher(subjectString); 
    while (regexMatcher.find()) { 
     for (int i = 1; i <= regexMatcher.groupCount(); i++) { 
      // matched text: regexMatcher.group(i) 
      // match start: regexMatcher.start(i) 
      // match end: regexMatcher.end(i) 

      // I suppose here you must use something like 
      // sstr += regexMatcher.group(i) + " " 
     } 
    } 
} catch (PatternSyntaxException ex) { 
    // Syntax error in the regular expression 
} 

至少,它似乎在Python中工作正常:

import re 

text = """ 
este es un texto de prueba "para ver como se comporta " la funcion sobre esto 
"para ver como se comporta " la funcion sobre esto "o sobre otro" lo q sea 
""" 

ret = "" 
print text 

reobj = re.compile(r'([^\" ])*(\".*?\")*', re.IGNORECASE) 

for match in reobj.finditer(text): 
    if match.group() <> "": 
     ret = ret + match.group() + "|" 

print ret 
0

解析出引用的內容後,運行這對其餘的,散裝或一塊一塊的必要:

String text = "ABC DEF GHI JKL"; 
text = text.replaceAll("()+", " "); 
// text: "ABC DEF GHI JKL" 
0

傑夫,你在正確的軌道上,但也有一些錯誤在你的代碼,即:(1)你忘了逃避否定字符類中的引號; (2)第一捕獲組內的人體應該是非捕獲變體; (3)如果第二組捕捉夥伴不參與比賽,則group(2)返回空值,並且您沒有爲此進行測試; (4)如果在正則表達式中測試兩個或多個空格而不是一個或多個,則不需要稍後檢查匹配的長度。下面是修改後的代碼:

import java.util.regex.*; 

public class Test 
{ 
    public static void main(String[] args) throws Exception 
    { 
    String text = "blah blah \"boo boo boo\" blah blah"; 
    Pattern p = Pattern.compile("(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|( +)"); 
    StringBuffer sb = new StringBuffer(); 
    Matcher m = p.matcher(text); 
    while (m.find()) 
    { 
     if (m.group(2) != null) 
     { 
     m.appendReplacement(sb, " "); 
     } 
    } 
    m.appendTail(sb); 
    System.out.println(sb.toString()); 
    } 
} 
+0

@Alan - 謝謝。我相應地更新了我的答案。 – 2008-11-05 06:34:49

相關問題