2009-03-23 4 views
7

मैं एक पैटर्न (जावा) शब्दों की एक दी गई सूची को छोड़कर वर्णों के किसी अनुक्रम मैच के लिए कैसे लिख सकता हूँ?जावा पैटर्न एक दी गई सूची को छोड़कर वर्णों के किसी अनुक्रम मैच के लिए

मैं अगर किसी दिए गए कोड टैग से घिरा किसी भी पाठ है खोजने के लिए शब्दों की एक दी गई सूची के अलावा तरह की जरूरत है। उदाहरण के लिए, मैं अगर वहाँ "एक" और "दो" टैग के अतिरिक्त कोई अन्य शब्द हैं जाँच करना चाहते हैं।

"This is the first tag <span>one</span> and this is the third <span>three</span>" 

पैटर्न ऊपर स्ट्रिंग से मेल खाना चाहिए क्योंकि शब्द "तीन" टैग से घिरा है और दिए गए शब्दों की सूची ("एक", "दो") का हिस्सा नहीं है।

+0

आप को पहचान आपकी संपादित पहले बनाम अब अपने प्रश्न के बीच इस तरह के एक आवश्यक अंतर यह है कि, है कि आपके संपादन से पहले ही जवाब दे किसी को भी अनिवार्य रूप से अपने समय बर्बाद करें? अगली बार शुरुआत से बिल्कुल स्पष्ट होने का प्रयास करें। यहां कोई भी क्रिस्टल बॉल नहीं है या आपके दिमाग को पढ़ सकता है। – Tomalak

उत्तर

2

इस का उपयोग करें:

if (!Pattern.matches(".*(word1|word2|word3).*", "word1")) { 
    System.out.println("We're good."); 
}; 

आप जाँच कर रहे हैं कि पैटर्न नहीं स्ट्रिंग से मेल नहीं करता है।

+0

आपके लिए प्रतिक्रिया के लिए धन्यवाद लेकिन यह काम नहीं करेगा। मैंने समस्या के विवरण के लिए और जानकारी जोड़ा। – Mario

7

देखो आगे कर सकते हैं:

\b(?!your|given|list|of|exclusions)\w+\b 

  • मेल एक शब्द सीमा (स्टार्ट-की-शब्द)
  • की "अपने" "दिया" किसी भी द्वारा पीछा नहीं , "सूची", "का", "बहिष्करण"
  • कई शब्द वर्ण
  • के बाद एक शब्द सीमा (अंत-शब्द)

वास्तव में, यह किसी भी शब्द है कि शामिल नहीं है मेल खाता है।

4

यह आप आरंभ करना चाहिए।

import java.util.regex.*; 

// >(?!one<|two<)(\w+)/ 
// 
// Match the character “>” literally «>» 
// Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!one|two)» 
// Match either the regular expression below (attempting the next alternative only if this one fails) «one» 
//  Match the characters “one<” literally «one» 
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «two» 
//  Match the characters “two<” literally «two» 
// Match the regular expression below and capture its match into backreference number 1 «(\w+)» 
// Match a single character that is a “word character” (letters, digits, etc.) «\w+» 
//  Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
// Match the characters “/” literally «</» 
List<String> matchList = new ArrayList<String>(); 
try { 
    Pattern regex = Pattern.compile(">(?!one<|two<)(\\w+)/"); 
    Matcher regexMatcher = regex.matcher(subjectString); 
    while (regexMatcher.find()) { 
     matchList.add(regexMatcher.group(1)); 
    } 
} catch (PatternSyntaxException ex) { 
    // Syntax error in the regular expression 
} 
+0

मुझे लगता है कि आप "एक" और "दो" पैटर्न को बदलने के लिए चाहते हो सकता है करने के लिए "एक <" और "दो <" तो आप अभी भी चीजें हैं जो उन दोनों में से किसी के साथ शुरू से मेल कर सकते हैं। –

+0

@ मार्टी - आप सही हैं। मैं जवाब अपडेट करूंगा। –

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