问题
What is the exact equivalent of PHP preg_match($pattern, $text) in JAVA ?
I got different result for same text input as well as regex pattern in following php & java program.
Regex - \b(?:(?>cancer()|problem()|(?>\1|\2)\w+)\b\W*?){0,3}\1\2 do match term -"cancer problem" within number Of words. here this is {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>';
}
?>
What is the exact equivalent of JAVA implementation of above program ?
I got different result for same text input as well as regex pattern in following java program.
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");
}
}
}
回答1:
I made a solution for this. hope this may be a clue for other expertise in 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));
}
}
来源:https://stackoverflow.com/questions/45598271/java-php-preg-match