如何在Java正则表达式中使用反向引用

时间:2014-08-22 05:48:52

标签: java regex

RE专家的问题:考虑以下Perl脚本:

my @lines = (
        "Once upon a time in a galaxy far, far away, there lived\n",
        "this _idiot_ trying to _mark up_ a few lines of\n",
        "marked down text using yet another _language_.\n");

foreach (@lines) {
        s|_(.+?)_|<em>$1</em>|g;
        print
}

%perl [aboveScript]的输出是

Once upon a time in a galaxy far, far away, there lived
this <em>idiot</em> trying to <em>mark up</em> a few lines of
marked down text using yet another <em>language</em>.

我试图在Java中实现这一点。我提出的课程如下。它工作,我得到与上面相同的输出,但我很确定这不是这样做的方式。我的问题 - 你将如何实现“parseLine()”方法?

import java.util.*;
import java.util.regex.*;

public class Reglob {

        private final static Pattern emPattern = Pattern.compile ("_(.+?)_");

        public void parseLine (String[] lines) {
                for (String line : lines) {
                        List<Integer>   bList = new ArrayList<Integer>(),
                                        eList = new ArrayList<Integer>();
                        Matcher m = emPattern.matcher (line);
                        int n = 0;
                        while (m.find()) {
                                // System.out.println ("Match indices: " + m.start() + ", " + m.end());
                                bList.add (m.start());
                                eList.add (m.end());
                                n++;
                        }
                        if (n == 0) {
                                System.out.println (line);
                        } else {
                                String s = line.substring (0, bList.get(0));
                                for (int i = 0 ; i < n-1 ; i++) {
                                    s += "<em>"
                                        + line.substring(1+bList.get(i),eList.get(i)-1)
                                        + "</em>" + line.substring (eList.get(i), bList.get(i+1));
                                }
                                s += "<em>"
                                        + line.substring(1+bList.get(n-1),eList.get(n-1)-1)
                                        + "</em>" + line.substring (eList.get(n-1), line.length());
                                System.out.println (s);
        }}}

        public static void main (String[] args) {
                String[] lines = {
                        "Once upon a time in a galaxy far, far away, there lived",
                        "this _idiot_ trying to _mark up_ a few lines of",
                        "marked down text using yet another _language_."};
                new Reglob().parseLine (lines);
}}

4 个答案:

答案 0 :(得分:2)

这是Perl脚本的Java等价物:

public class Main {
    public static void main(String[] args) {
        String[] lines = {
                "Once upon a time in a galaxy far, far away, there lived\n",
                "this _idiot_ trying to _mark up_ a few lines of\n",
                "marked down text using yet another _language_.\n" };

        for(String line : lines) {
            String output = line.replaceAll("_(.+?)_", "<em>$1</em>");

            System.out.print(output);
        }
    }
}

输出:

Once upon a time in a galaxy far, far away, there lived
this <em>idiot</em> trying to <em>mark up</em> a few lines of
marked down text using yet another <em>language</em>.

答案 1 :(得分:1)

你可以这样做,

String [] s = {  "Once upon a time in a galaxy far, far away, there lived",
                "this _idiot_ trying to _mark up_ a few lines of",
                 "marked down text using yet another _language_."};
for(String s2 : s)
{
System.out.println(s2.replaceAll("_([^_]+)_", "<em>$1</em>"));
}

答案 2 :(得分:0)

尝试这样的事情:

public static final String str = "Once upon a time in a galaxy far, far away, there lived\n" +
        "this _idiot_ trying to _mark up_ a few lines of\n" +
        "marked down text using yet another _language_.\n";

public static void main(String[] args) {
    System.out.println(str.replaceAll("_(.+?)_", "<em>$1</em>"));
}

此输出:

Once upon a time in a galaxy far, far away, there lived
this <em>idiot</em> trying to <em>mark up</em> a few lines of
marked down text using yet another <em>language</em>.

答案 3 :(得分:0)

同样,在Scala中:

scala> val text = """Once upon a time in a galaxy far, far away, there lived
     | this _idiot_ trying to _mark up_ a few lines of
     | marked down text using yet another _language_."""
text: String =
Once upon a time in a galaxy far, far away, there lived
this _idiot_ trying to _mark up_ a few lines of
marked down text using yet another _language_.

scala> val r = "_(.+?)_".r
r: scala.util.matching.Regex = _(.+?)_

scala> r.replaceAllIn(text, """<em>$1<\em>""")
res3: String =
Once upon a time in a galaxy far, far away, there lived
this <em>idiot<em> trying to <em>mark up<em> a few lines of
marked down text using yet another <em>language<em>.

我首先在Scala REPL中尝试所有内容,然后转换为Java。

但是既然你有很多Java答案,我就会回到Hulu。

相关问题