[英]Java & PHP - preg_match
在JAVA中,PHP preg_match($ pattern,$ text)的確切含義是什么?
在以下php&java程序中,對於相同的文本輸入以及regex模式,我得到了不同的結果。
正則表達式-\\ b(?:(?> cancer()| problem()|(?> \\ 1 | \\ 2)\\ w +)\\ b \\ W *?){0,3} \\ 1 \\ 2做匹配項-字數以內的“癌症問題”。 這是{0,3}
<?php
$text = "doctors found many cancer related chest problems in japan during second world war.";
$pattern = "/\\b(?:(?>cancer()|problem()|(?>\\1|\\2)\\w+)\\b\\W*?){0,3}\\1\\2/i";
if (preg_match($pattern, $text)) {
echo 'matched<br>';
} else {
echo 'not matched<br>';
}
?>
以上程序的JAVA實現的確切含義是什么?
package com.regex.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchTermWithin_1 {
final static String string = "doctors found many cancer related chest problems in japan during second world war.";
final static String regex = "\\b(?:(?>cancer()|problem()|(?>\\1|\\2)\\w+)\\b\\W*?){0,3}\\1\\2";
public static void main(String[] args) {
// TODO Auto-generated method stub
final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println("Full match: " + matcher.group(0)+"\n");
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
System.out.println("\n");
}else {
System.out.println("not found");
}
if(matcher.matches()) {
System.out.println("matched "+"\n");
}else {
System.out.println("not matched " + "\n");
}
}
}
我為此提出了解決方案。 希望這可能是其他Java和PHP專業知識的線索。
package com.ae.text.processor;
import org.apache.commons.lang3.ArrayUtils;
public class ProximitySearch {
public static boolean MatchTermWithinNumberOfWords(String text, String term, int distance) {
String[] term_words = term.split(" ");
String[] right_side_array;
String right_side = "";
String tmp = "";
if (text.contains(term_words[0])) {
String[] splitted_search_words = text.split(term_words[0]);
for (int i = 0; i < splitted_search_words.length; i++) {
if (i == 1) {
right_side = splitted_search_words[1];
if (right_side.contains(term_words[1])) {
right_side_array = right_side.split(" ");
int indexOfSecondTerm = ArrayUtils.indexOf(right_side_array, term_words[1]);
for (int j = 0; j <= indexOfSecondTerm; j++) {
tmp = tmp + " " + right_side_array[j];
}
if (indexOfSecondTerm <= distance) {
printMatched(term_words[0] + tmp);
return true;
} else {
return false;
}
}
}
}
}
return false;
}
public static void printMatched(String mathed) {
System.out.println(mathed);
}
public static void main(String[] args) {
String text = "doctors found many cancer related chest problems in japan durring second world war.";
String term = "cancer problems";
int distance = 3;
System.out.println(MatchTermWithinNumberOfWords(text, term, distance));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.