preg_replace在java中,Java相当于PHP的preg_replace_callback

I'm in the process of moving an application from PHP to Java and there is heavy use of regular expressions in the code. I've run across something in PHP that doesn't seem to have a java equivalent:

preg_replace_callback()

For every match in the regex, it calls a function that is passed the match text as a parameter. As an example usage:

$articleText = preg_replace_callback("/\[thumb(\d+)\]/",'thumbReplace', $articleText);

# ...

function thumbReplace($matches) {

global $photos;

return "%22";

}

What would be the ideal way to do this in Java?

解决方案

IMPORTANT: As pointed out by Kip in the comments, this class has an infinite loop bug if the matching regex matches on the replacement string. I'll leave it as an exercise to readers to fix it, if necessary.

I don't know of anything similar that's built into Java. You could roll your own without too much difficulty, using the Matcher class:

import java.util.regex.*;

public class CallbackMatcher

{

public static interface Callback

{

public String foundMatch(MatchResult matchResult);

}

private final Pattern pattern;

public CallbackMatcher(String regex)

{

this.pattern = Pattern.compile(regex);

}

public String replaceMatches(String string, Callback callback)

{

final Matcher matcher = this.pattern.matcher(string);

while(matcher.find())

{

final MatchResult matchResult = matcher.toMatchResult();

final String replacement = callback.foundMatch(matchResult);

string = string.substring(0, matchResult.start()) +

replacement + string.substring(matchResult.end());

matcher.reset(string);

}

}

}

Then call:

final CallbackMatcher.Callback callback = new CallbackMatcher.Callback() {

public String foundMatch(MatchResult matchResult)

{

return "%22";

}

};

final CallbackMatcher callbackMatcher = new CallbackMatcher("/\[thumb(\d+)\]/");

callbackMatcher.replaceMatches(articleText, callback);

Note that you can get the entire matched string by calling matchResults.group() or matchResults.group(0), so it's not necessary to pass the callback the current string state.

EDIT: Made it look more like the exact functionality of the PHP function.

Here's the original, since the asker liked it:

public class CallbackMatcher

{

public static interface Callback

{

public void foundMatch(MatchResult matchResult);

}

private final Pattern pattern;

public CallbackMatcher(String regex)

{

this.pattern = Pattern.compile(regex);

}

public String findMatches(String string, Callback callback)

{

final Matcher matcher = this.pattern.matcher(string);

while(matcher.find())

{

callback.foundMatch(matcher.toMatchResult());

}

}

}

For this particular use case, it might be best to simply queue each match in the callback, then afterwards run through them backwards. This will prevent having to remap indexes as the string is modified.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值