2017-09-14 112 views
0

正如所描述的標題,正則表達式應提供給定字符串,字符串前綴(可選)和字符串後綴(可選)的提取信息的目的如何使正則表達式允許可選的前綴和後綴提取

這樣

prefix_group_1_suffix回報group_1時的前綴是 'prefix_' 和後綴是_suffix

prefix_group_1回報group_1時的前綴是 'prefix_' 和後綴null < - 我的代碼無法處理這種情況

group_1_suffix回報group_1時前綴爲「空」和後綴是_suffix

group_1回報group_1時前綴爲「空」和後綴是null < - 我的代碼不能處理這種情況

這裏是我的代碼,但是我發現它不工作時

String itemName = ""; 
    String prefix = "TEST_"; 
    String suffix = ""; 
    String itemString = prefix + "item_1" + suffix; 
    String prefix_quote = "".equals(prefix) ? "" : Pattern.quote(prefix); 
    String suffix_quote = "".equals(suffix) ? "" : Pattern.quote(suffix); 
    String regex = prefix_quote + "(.*?)" + suffix_quote; 
    Pattern pattern = Pattern.compile(regex); 
    Matcher matcher = pattern.matcher(itemString); 
    while (matcher.find()) { 
     itemName = matcher.item(1); 
     break; 
    } 
    System.out.println("itemString '"+itemString+"'"); 
    System.out.println("Prefix quote '"+prefix_quote+"'"); 
    System.out.println("Suffix quote '"+suffix_quote+"'"); 
    System.out.println("regex '"+regex+"'"); 
    System.out.println("itemName is '"+itemName+"'"); 

,這裏是輸出

itemString 'TEST_item_1' 
Prefix quote '\QTEST_\E' 
Suffix quote '' 
regex '\QTEST_\E(.*?)' 
itemName is '' 

但上面的代碼工作以及與其他兩個條件

+0

你看的正則表達式的任何教程? – JoelFan

+0

那麼考慮兩種情況,prefix_group_1和group_1_suffix。我相信前綴和後綴可以是任何文本值。然後這兩種模式都與A_B_C相同。系統如何說如果A是前綴,B_C是缺少後綴的數據,或者C是後綴爲A_B的後綴,並且缺少前綴。該系統需要更多信息。另外,如果你的文本是用下劃線格式化的,那麼爲什麼你需要使用正則表達式的原因是什麼?爲什麼不把它解析爲標記。 – Gautam

+0

你爲什麼要重複比賽?我理解你的問題的方式是,每個字符串只能有(最多).one匹配。 –

回答

0

爲什麼你的代碼失敗就在於懶惰量詞.*?原因。最重要的是儘可能少的匹配,最好是空字符串,所以它就是這樣做的。因此,您需要將正則表達式錨定到字符串的開始/結尾以及可能的前綴/後綴。

對於這一點,你可以使用lookaround assertions

String prefix = "TEST_"; 
String suffix = ""; 
String itemString = prefix + "item_1" + suffix; 
String prefix_quote = "".equals(prefix) ? "^" : Pattern.quote(prefix); 
String suffix_quote = "".equals(suffix) ? "$" : Pattern.quote(suffix); 
String regex = "(?<=^|" + prefix_quote + ")(.*?)(?=$|" + suffix_quote + ")"; 
Pattern pattern = Pattern.compile(regex); 
Matcher matcher = pattern.matcher(itemString); 

這將導致在正則表達式

(?<=^|TEST_)item_1(?=$|$) 

說明:

(?<= # Assert that it's possible to match before the current position 
^  # either the start of the string 
|  # or 
TEST_ # the prefix 
)  # End of lookbehind 
item_1 # Match "item_1" 
(?=$|$) # Assert that it's possible to match after the current position 
     # either the end of the string or the suffix (which is replaced 
     # by the end of the string if empty. Of course that could be optimized 
     # when constructing the regex, this is just a quick-and-dirty solution). 
+0

如果'^'和'$'錨點是合適的,這意味着正則表達式用於*匹配*,而不是*找到*,所以放下錨點並簡單地調用'matches()'而不是'find() '。 – Andreas

+0

這是行不通的,因爲lookaround斷言實際上並不匹配文本; '.matches()'要求整個字符串由正則表達式匹配。請注意,錨點位於lookaround斷言的內部,而不是在正則表達式的開始/結束處。我確實認爲OP需要確認他的目標確實匹配,而不是找到。 –

-1

,如果你有,你想找到一個特定的字符串,那麼你可以使用任何字符串匹配算法:

1.「boyer moore horspool」算法是kmp sring匹配算法的一個更好的版本。你可以試着找到你想要搜索的字符串的位置。 2.你也可以看看模糊字符串匹配的「Levenshtein距離」。

3.i猜測在字符串中找到一個子字符串將是一個更好的選擇。

碼有每一個地方....

+0

這似乎沒有回答這個問題。 –

相關問題