Java & PHP - preg_match



在JAVA中PHP preg_match($pattern,$text(的确切等价物是什么?

对于相同的文本输入以及遵循php和java程序的正则表达式模式,我得到了不同的结果。

正则表达式 - \b(?:(?>cancer((|problem((|(?>\1|\2(\w+(\b\W*?{0,3}\1\2 在字数内匹配术语 - "癌症问题"。这里是 {0,3}

.PHP

<?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实现的确切等价物是什么?

在以下 java 程序中,相同的文本输入以及正则表达式模式得到了不同的结果。

.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));
}
}

最新更新