मैं जब पाठ में खोज शब्द की तरह है के लिए एक भी बदतर समस्या में पड़ गए .NET
, C++
, C#
, और C
। आपको लगता होगा कि कंप्यूटर प्रोग्रामर किसी ऐसी भाषा का नाम लेना बेहतर जानते हैं, जिसके लिए नियमित अभिव्यक्ति लिखना कठिन हो।
वैसे भी, यह मुझे पता चला है (संक्षेप में ज्यादातर http : //www. अनियमित-expressions.info , जो एक महान साइट है): रेगेक्स के अधिकांश स्वादों में, जो चरित्र शॉर्ट-हैंड चरित्र वर्ग द्वारा मेल खाते \w
हैं वे हैं वर्ण जो शब्द सीमाओं द्वारा शब्द वर्ण के रूप में माने जाते हैं। जावा एक अपवाद है। जावा यूनिकोड का समर्थन करता है \b
लेकिन इसके लिए नहीं \w
। (मुझे यकीन है कि उस समय इसका एक अच्छा कारण था)।
\w
"शब्द चरित्र" के लिए खड़ा है। यह हमेशा ASCII वर्णों से मेल खाता है [A-Za-z0-9_]
। अंडरस्कोर और अंकों (लेकिन डैश नहीं!) को शामिल करने पर ध्यान दें। अधिकांश स्वादों में जो यूनिकोड का समर्थन करते हैं, \w
उनमें अन्य लिपियों के कई पात्र शामिल हैं। इसमें बहुत सारी विसंगति है कि कौन से पात्र वास्तव में शामिल हैं। वर्णमाला लिपियों और विचारधाराओं के पत्र और अंक आम तौर पर शामिल होते हैं। अंडरस्कोर और संख्यात्मक प्रतीकों के अलावा कनेक्टर विराम चिह्न जो अंक नहीं हैं या शामिल नहीं हो सकते हैं। XML स्कीमा और XPath में भी सभी प्रतीकों को शामिल किया गया है \w
। लेकिन जावा, जावास्क्रिप्ट और PCRE केवल ASCII वर्णों के साथ मेल खाते हैं\w
।
यही कारण है कि जावा-आधारित रेगेक्स खोज करता है C++
, C#
या .NET
(जब आप अवधि और प्लस से बचने के लिए याद करते हैं) द्वारा खराब कर दिया जाता है \b
।
नोट: मुझे यकीन नहीं है कि पाठ में गलतियों के बारे में क्या करना है, जैसे कि जब किसी ने वाक्य के अंत में एक अवधि के बाद कोई स्थान नहीं रखा है। मैंने इसके लिए अनुमति दी थी, लेकिन मुझे यकीन नहीं है कि यह आवश्यक रूप से सही काम है।
वैसे भी, जावा में, यदि आप उन अजीब-नाम वाली भाषाओं के लिए पाठ खोज रहे हैं, तो आपको \b
व्हाट्सएप और विराम चिह्नों से पहले और बाद में इसे बदलने की आवश्यकता है । उदाहरण के लिए:
public static String grep(String regexp, String multiLineStringToSearch) {
String result = "";
String[] lines = multiLineStringToSearch.split("\\n");
Pattern pattern = Pattern.compile(regexp);
for (String line : lines) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
result = result + "\n" + line;
}
}
return result.trim();
}
फिर अपने परीक्षण या मुख्य कार्य में:
String beforeWord = "(\\s|\\.|\\,|\\!|\\?|\\(|\\)|\\'|\\\"|^)";
String afterWord = "(\\s|\\.|\\,|\\!|\\?|\\(|\\)|\\'|\\\"|$)";
text = "Programming in C, (C++) C#, Java, and .NET.";
System.out.println("text="+text);
// Here is where Java word boundaries do not work correctly on "cutesy" computer language names.
System.out.println("Bad word boundary can't find because of Java: grep with word boundary for .NET="+ grep("\\b\\.NET\\b", text));
System.out.println("Should find: grep exactly for .NET="+ grep(beforeWord+"\\.NET"+afterWord, text));
System.out.println("Bad word boundary can't find because of Java: grep with word boundary for C#="+ grep("\\bC#\\b", text));
System.out.println("Should find: grep exactly for C#="+ grep("C#"+afterWord, text));
System.out.println("Bad word boundary can't find because of Java:grep with word boundary for C++="+ grep("\\bC\\+\\+\\b", text));
System.out.println("Should find: grep exactly for C++="+ grep(beforeWord+"C\\+\\+"+afterWord, text));
System.out.println("Should find: grep with word boundary for Java="+ grep("\\bJava\\b", text));
System.out.println("Should find: grep for case-insensitive java="+ grep("?i)\\bjava\\b", text));
System.out.println("Should find: grep with word boundary for C="+ grep("\\bC\\b", text)); // Works Ok for this example, but see below
// Because of the stupid too-short cutsey name, searches find stuff it shouldn't.
text = "Worked on C&O (Chesapeake and Ohio) Canal when I was younger; more recently developed in Lisp.";
System.out.println("text="+text);
System.out.println("Bad word boundary because of C name: grep with word boundary for C="+ grep("\\bC\\b", text));
System.out.println("Should be blank: grep exactly for C="+ grep(beforeWord+"C"+afterWord, text));
// Make sure the first and last cases work OK.
text = "C is a language that should have been named differently.";
System.out.println("text="+text);
System.out.println("grep exactly for C="+ grep(beforeWord+"C"+afterWord, text));
text = "One language that should have been named differently is C";
System.out.println("text="+text);
System.out.println("grep exactly for C="+ grep(beforeWord+"C"+afterWord, text));
//Make sure we don't get false positives
text = "The letter 'c' can be hard as in Cat, or soft as in Cindy. Computer languages should not require disambiguation (e.g. Ruby, Python vs. Fortran, Hadoop)";
System.out.println("text="+text);
System.out.println("Should be blank: grep exactly for C="+ grep(beforeWord+"C"+afterWord, text));
PS मेरा धन्यवाद http://regexpal.com/ जिसके बिना रेगेक्स की दुनिया बहुत दयनीय होगी!