2013-08-15 8 views
6

में नई लाइन समेत है, मैं एक वेबसाइट से प्राप्त पाठ्यपुस्तक परिभाषाओं के लिए नियमित अभिव्यक्ति से मिलान करने की कोशिश कर रहा हूं। परिभाषा के बाद परिभाषा हमेशा एक नई पंक्ति के साथ शब्द है। उदाहरण के लिए:जावा रेगेक्स मैच

Zither 
Definition: An instrument of music used in Austria and Germany It has from thirty to forty wires strung across a shallow sounding board which lies horizontally on a table before the performer who uses both hands in playing on it Not to be confounded with the old lute shaped cittern or cithern 

मेरे प्रयास में बस मैं न्यू लाइन चरित्र बार आ रही है (इस मामले "जिट्रा" में) शब्द मिलता है।

मैंने बिना किसी किस्मत के ^(\w+)\s और ^(\S+)\s दोनों की कोशिश की। मैंने सोचा कि शायद ^(\S+)$ काम करेगा, लेकिन ऐसा लगता है कि यह शब्द सफलतापूर्वक मेल नहीं खाता है। मैं rubular, http://rubular.com/r/LPEHCnS0ri के साथ परीक्षण कर रहा हूँ; ऐसा लगता है कि जावा मेरे द्वारा किए जाने वाले तथ्य के बावजूद, मेरे सभी प्रयासों को सफलतापूर्वक मेल नहीं खाता है।

यहाँ मेरी टुकड़ा

String str = ...; //Here the string is assigned a word and definition taken from the internet like given in the example above. 
Pattern rgx = Pattern.compile("^(\\S+)$"); 
Matcher mtch = rgx.matcher(str); 
if (mtch.find()) { 
    String result = mtch.group(); 
    terms.add(new SearchTerm(result, System.nanoTime())); 
} 

यह आसानी से जिसके परिणामस्वरूप स्ट्रिंग triming द्वारा हल किया जाता है, लेकिन वह जैसे कि यह अनावश्यक हो अगर मैं पहले से ही एक नियमित अभिव्यक्ति का उपयोग कर रहा चाहिए लगता है।

सभी मदद की बहुत सराहना की जाती है। अग्रिम में धन्यवाद!

+2

'\ s' मैचों' \ n' दूसरों के बीच। –

उत्तर

8

, Pattern.MULTILINE विकल्प

Pattern rgx = Pattern.compile("^(\\S+)$", Pattern.MULTILINE); 

यह आपके स्ट्रिंग में लाइन सीमांकक पहचान करने के लिए regex का कारण बनता है का उपयोग कर अन्यथा ^ और $ सिर्फ प्रारंभ और स्ट्रिंग के अंत से मेल की कोशिश करो।

हालांकि यह इस पद्धति के लिए कोई फर्क नहीं पड़ता, Matcher.group() विधि जबकि Matcher.group(int) विधि विशेष कैप्चर समूह (...) संख्या आपके द्वारा निर्दिष्ट के आधार पर की मैच रिटर्न पूरे मैच देता है,। आपका पैटर्न एक कैप्चर समूह निर्दिष्ट करता है जो आप कैप्चर करना चाहते हैं। यदि आपने लिखा था कि आपने अपने पैटर्न में \s शामिल किया था, तो Matcher.group() में उस वापसी मूल्य में व्हाइटस्पेस शामिल होगा।

+0

यह चाल है। धन्यवाद, मुझे एहसास नहीं हुआ कि आपको कई लाइनें निर्दिष्ट करनी होंगी। –

0

अगले का प्रयास करें:

/* The regex pattern: ^(\w+)\r?\n(.*)$ */ 
private static final REGEX_PATTERN = 
     Pattern.compile("^(\\w+)\\r?\\n(.*)$"); 

public static void main(String[] args) { 
    String input = "Zither\n Definition: An instrument of music"; 

    System.out.println(
     REGEX_PATTERN.matcher(input).matches() 
    ); // prints "true" 

    System.out.println(
     REGEX_PATTERN.matcher(input).replaceFirst("$1 = $2") 
    ); // prints "Zither = Definition: An instrument of music" 

    System.out.println(
     REGEX_PATTERN.matcher(input).replaceFirst("$1") 
    ); // prints "Zither" 
} 
2

नियमित अभिव्यक्ति के साथ पहले समूह हमेशा पूरा मिलता जुलता स्ट्रिंग है। आपके मामले में आप समूह 1 चाहते हैं, नहीं समूह 0.

तो mtch.group(1) करने के लिए mtch.group() बदलते चाल करना चाहिए:

String str = ...; //Here the string is assigned a word and definition taken from the internet like given in the example above. 
Pattern rgx = Pattern.compile("^(\\w+)\s"); 
Matcher mtch = rgx.matcher(str); 
if (mtch.find()) { 
    String result = mtch.group(1); 
    terms.add(new SearchTerm(result, System.nanoTime())); 
} 
+0

मुझे इसे मारो। 1 +। –

+0

+1 आप दोनों को मारने के लिए +1 :) – anubhava

+0

यह जानना अच्छा है, मैं वास्तव में उस जानकारी की सराहना करता हूं! –

1

बस की जगह:

String result = mtch.group(); 

द्वारा:

String result = mtch.group(1); 

यह आपके आउटपुट को capturing group (उदा। । (\\w+))।

1

एक देर से प्रतिक्रिया, लेकिन अगर आप पैटर्न और Matcher उपयोग नहीं कर रहे हैं, तो आप DOTALL के इस विकल्प आपके regex स्ट्रिंग में

(?s)[Your Expression] 

मूल रूप से (?s) भी डॉट बताता सभी वर्णों का मिलान करने सहित लाइन टूट जाता है

उपयोग कर सकते हैं,

विस्तृत जानकारी: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

संबंधित मुद्दे