Java 如何在使用多个模式时查找当前正则表达式以进行一次又一次的模式匹配。find()?
我正在使用下面的正则表达式替换匹配的内容。Java 如何在使用多个模式时查找当前正则表达式以进行一次又一次的模式匹配。find()?,java,regex,match,Java,Regex,Match,我正在使用下面的正则表达式替换匹配的内容。 我的问题是,有没有办法找到当前matcher正在使用的模式(我指的是matcher.find()用于给定模式中的哪一个) String[]searchRegex=newstring[]{“(?i)]+)/>”,“(?i)]+)/>”,“\\$var([^>]+)\$”; StringBuffer=新的StringBuffer(); Pattern Pattern=Pattern.compile(Stream.of(searchRegex).collec
我的问题是,有没有办法找到当前matcher正在使用的模式(我指的是matcher.find()用于给定模式中的哪一个)
String[]searchRegex=newstring[]{“(?i)]+)/>”,“(?i)]+)/>”,“\\$var([^>]+)\$”;
StringBuffer=新的StringBuffer();
Pattern Pattern=Pattern.compile(Stream.of(searchRegex).collect(joining(“|”);
Matcher Matcher=pattern.Matcher(jsonObject.toString());
while(matcher.find()){
//如何找到此处使用的模式来匹配内容?
String match=matcher.group();
系统输出打印项次(匹配);
//这里,需要为每个模式使用不同的替换内容
matcher.appendReplacement(缓冲区,matcher.quoteReplacement(“”);
}
matcher.appendTail(缓冲区);
您的最终正则表达式如下所示
(?i)<my:link([^>]+)/>|(?i)<my:anotherLink([^>]+)/>
^^^^^^^ ^^^^^^^
group 1 group 2
输出:
ab was matched by first pattern
ac was matched by second pattern
你最后的正则表达式看起来像
(?i)<my:link([^>]+)/>|(?i)<my:anotherLink([^>]+)/>
^^^^^^^ ^^^^^^^
group 1 group 2
输出:
ab was matched by first pattern
ac was matched by second pattern
你应该使用分组。通过将正则表达式的一个区域括在括号中来定义组 然后你会:
m.find();
m.group(0); // the entire expression matched
m.group(1); // the first group, you start counting with index 1
不过,我建议您尝试一下谷歌的REJ2 matcher
您可以命名子组,下面是一个带有命名组的正则表达式示例:
private static final String HTML_REGEX =
"(?is)<td class=\"section_post_header\" colspan=\"2\"><h1><span style.*?>(?<displaynamefallback>.*?)</span></h1></td>.*?" +
"Download Links.*?" +
".*<a href=\"(?<magneturl>magnet:\\?.*?)\" class=\"magnet\".*?" +
//"(<a href=\"(?<magneturl>magnet:\\?.*?)\" title=\"Magnet Link\".*?)?"+
".*<a href=\"(?<torrenturl>http(s)?.*?\\.torrent)\" class=\"download_.\".*?" +
"(Torrent Info.*?title=\"(?<displayname>.*?)\".*?)?" +
"(<b>Torrent File:</b>\\s+(?<displayname2>.*?)<br.*?)?" +
"(<b>Torrent Hash:</b>\\s+(?<infohash>.*?)<br.*?)?" +
"<b>Filesize:</b>\\s+(?<filesize>.*?)<br.*?" +
"<b>Released:</b>\\s+(?<creationtime>.*?)<br";
如果您删除了一个组,则不必在代码中更新其余组的位置。您应该使用组。通过将正则表达式的一个区域括在括号中来定义组 然后你会:
m.find();
m.group(0); // the entire expression matched
m.group(1); // the first group, you start counting with index 1
不过,我建议您尝试一下谷歌的REJ2 matcher
您可以命名子组,下面是一个带有命名组的正则表达式示例:
private static final String HTML_REGEX =
"(?is)<td class=\"section_post_header\" colspan=\"2\"><h1><span style.*?>(?<displaynamefallback>.*?)</span></h1></td>.*?" +
"Download Links.*?" +
".*<a href=\"(?<magneturl>magnet:\\?.*?)\" class=\"magnet\".*?" +
//"(<a href=\"(?<magneturl>magnet:\\?.*?)\" title=\"Magnet Link\".*?)?"+
".*<a href=\"(?<torrenturl>http(s)?.*?\\.torrent)\" class=\"download_.\".*?" +
"(Torrent Info.*?title=\"(?<displayname>.*?)\".*?)?" +
"(<b>Torrent File:</b>\\s+(?<displayname2>.*?)<br.*?)?" +
"(<b>Torrent Hash:</b>\\s+(?<infohash>.*?)<br.*?)?" +
"<b>Filesize:</b>\\s+(?<filesize>.*?)<br.*?" +
"<b>Released:</b>\\s+(?<creationtime>.*?)<br";
如果您去掉了一个组,那么就不必在代码中更新其余组的位置