得到了不同的结果

得到了不同的结果

本文介绍了Java&php-preg_Match的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的PHP preg_Match($Pattern,$Text)的确切等价物是什么?

在下面的php&;Java程序中,对于相同的文本输入和正则表达式模式,我得到了不同的结果。

Regex-(?:(?>cancer()|problem()|(?>1|2)w+)W*?){0,3}12一定要在字数上匹配术语--"癌症问题"。这里是{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)+"
");
        for (int i = 0; i <= matcher.groupCount(); i++) {
            System.out.println("Group " + i + ": " + matcher.group(i));
        }
        System.out.println("
");
    }else {
        System.out.println("not found");
    }


    if(matcher.matches()) {
        System.out.println("matched "+"
");
    }else {
        System.out.println("not matched " + "
");
    }

    }
}

推荐答案

我为此制定了解决方案。希望这能为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));
    }
}

这篇关于Java&amp;php-preg_Match的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:52