2009-08-14 11 views
1

を交換可能な重複:
Java Regex Replace with Capturing GroupJavaの正規表現キャプチャグループと

はキャプチャグループの変更内容と正規表現を交換する方法はありますか?

例:

Pattern regex = Pattern.compile("(\\d{1,2})"); 
Matcher regexMatcher = regex.matcher(text); 
resultString = regexMatcher.replaceAll("$1"); // *3 ?? 

そして、私は3

編集を乗じた$ 1とのすべての発生を置き換えるしたい:何かが間違っている、のような

が見える:(

ご利用の場合

一致が

しかし

Pattern regex = Pattern.compile("(\\d{1,2})"); 
Matcher regexMatcher = regex.matcher("12 54 1 65"); 
try { 
    String resultString = regexMatcher.replaceAll("$1"); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

正常に動作を発見したが、私$ 77 :(

EDIT2変更することはできません:

それはIllegalStateExceptionがスロー今

を、それは働いています:)

+0

包みなさい、他のユーザー体験同様の問題は、あなたがこの問題を修正するために何をしたか手の込んだだろうか? :) – doomspork

+0

私はJavaでそれを行う方法を知りませんでしたが、Perlでは簡単です。 :-P $ foo = '12 54 1 65 '; $ foo =〜s /(\ d {1,2})/ $ 1 * 3/eg; –

+0

ここに答えがありましたが、私は今どこにいるのかわかりません...(私は問題を解決したが、消えた...) – user

答えて

14

この問題の決定的な解決策は、Elliott Hughes数年前にblogになりました。 Elliottはオンラインバージョンの他のクラスに無意味な依存関係を導入し続けているので、ここでスタンドアロンバージョンを投稿します(依存関係はmain()メソッドのテストにのみあります)。

import java.util.regex.*; 

/** 
* A Rewriter does a global substitution in the strings passed to its 
* 'rewrite' method. It uses the pattern supplied to its constructor, and is 
* like 'String.replaceAll' except for the fact that its replacement strings 
* are generated by invoking a method you write, rather than from another 
* string. This class is supposed to be equivalent to Ruby's 'gsub' when 
* given a block. This is the nicest syntax I've managed to come up with in 
* Java so far. It's not too bad, and might actually be preferable if you 
* want to do the same rewriting to a number of strings in the same method 
* or class. See the example 'main' for a sample of how to use this class. 
* 
* @author Elliott Hughes 
*/ 
public abstract class Rewriter 
{ 
    private Pattern pattern; 
    private Matcher matcher; 

    /** 
    * Constructs a rewriter using the given regular expression; the syntax is 
    * the same as for 'Pattern.compile'. 
    */ 
    public Rewriter(String regex) 
    { 
    this.pattern = Pattern.compile(regex); 
    } 

    /** 
    * Returns the input subsequence captured by the given group during the 
    * previous match operation. 
    */ 
    public String group(int i) 
    { 
    return matcher.group(i); 
    } 

    /** 
    * Overridden to compute a replacement for each match. Use the method 
    * 'group' to access the captured groups. 
    */ 
    public abstract String replacement(); 

    /** 
    * Returns the result of rewriting 'original' by invoking the method 
    * 'replacement' for each match of the regular expression supplied to the 
    * constructor. 
    */ 
    public String rewrite(CharSequence original) 
    { 
    this.matcher = pattern.matcher(original); 
    StringBuffer result = new StringBuffer(original.length()); 
    while (matcher.find()) 
    { 
     matcher.appendReplacement(result, ""); 
     result.append(replacement()); 
    } 
    matcher.appendTail(result); 
    return result.toString(); 
    } 



    public static void main(String... args) throws Exception 
    { 
    String str = "12 54 1 65"; 

    // anonymous subclass 
    Rewriter tripler = new Rewriter("(\\d{1,2})") 
    { 
     public String replacement() 
     { 
     int intValue = Integer.valueOf(group(1)); 
     return String.valueOf(intValue * 3); 
     } 
    }; 
    System.out.println(tripler.rewrite(str)); 

    // inline subclass 
    System.out.println(new Rewriter("(\\d{1,2})") 
    { 
     public String replacement() 
     { 
     int intValue = Integer.valueOf(group(1)); 
     return String.valueOf(intValue * 3); 
     } 
    }.rewrite(str)); 

    } 
} 
0

いいえ、できません正規表現でそれを行う。正規表現には数値の概念がないので、数字で算術演算を行うことはできません( "12 54 1 65"を "36 162 3 195"に変換すると仮定した場合)。

いくつかの言語と正規表現の実装ではこれを行うことができますが(PerlはChrisが投稿しました)、これは正規表現ではなく、特にJava-regexのものではありません。 あなたはすでに問題を解決していると言いましたので、あなたは "手動"の方法で各一致を整数に変換し、それに3を掛けたと思います。

関連する問題