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

标签 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 来实现这一点。我想出的类(class)如下。它有效,并且我得到与上面相同的输出,但我很确定这不是执行此操作的方法。我的问题 - 您将如何实现“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);
}}

最佳答案

这是 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>.

关于java - 如何在 Java 正则表达式中使用反向引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25440338/

相关文章:

java - 使用 spring 的比较器实例化优先级队列

java - Android 内部存储基准测试提供了难以置信的快速读取速度

java - 使用日历后验证文本框

regex - Apache .htaccess重写规则以删除.php文件扩展名

ruby - 如果单词没有出现在其他单词之前,则正则表达式匹配

iphone - NSPredicate中的单词边界(\b)导致NSFetchRequest不返回任何托管对象

java - 具有副作用的不可变对象(immutable对象)

java - 如何在不丢失或更改任何数据的情况下安全地将 RSA 加密和解密的字节转换为字符串?

java - 当您需要存储(非常)大的数字时该怎么办?

regex - 使用正则表达式搜索一个词的多次出现