com.xiaoleilu.hutool
类 ReUtil

java.lang.Object
  继承者 com.xiaoleilu.hutool.ReUtil

public class ReUtil
extends Object

正则相关工具类

作者:
xiaoleilu

字段摘要
static Pattern GROUP_VAR
           
static Pattern IPV4
           
static Pattern NUMBER
           
 
方法摘要
static String delFirst(String regex, String content)
          删除匹配的内容
static String delPreLocation(String regex, String content)
          删除正则匹配到的内容之前的字符 如果没有找到,则返回原文
static String extractMulti(Pattern pattern, String content, String template)
          从content中匹配出多个值并根据template生成新的字符串
例如:
content 2013年5月 pattern (.*?})
static String extractMulti(String regex, String content, String template)
          从content中匹配出多个值并根据template生成新的字符串
匹配结束后会删除匹配内容之前的内容(包括匹配内容)
例如:
content 2013年5月 pattern (.*?})
static String extractMultiAndDelPre(Pattern pattern, String[] contents, String template)
          从content中匹配出多个值并根据template生成新的字符串
匹配结束后会删除匹配内容之前的内容(包括匹配内容)
例如:
content 2013年5月 pattern (.*?})
static String extractMultiAndDelPre(String regex, String[] contents, String template)
          从content中匹配出多个值并根据template生成新的字符串
例如:
content 2013年5月 pattern (.*?})
static
<T extends Collection<String>>
T
findAll(Pattern pattern, String content, int group, T collection)
          取得内容中匹配的所有结果
static
<T extends Collection<String>>
T
findAll(String regex, String content, int group, T collection)
          取得内容中匹配的所有结果
static String get(Pattern pattern, String content, int groupIndex)
          获得匹配的字符串
static String get(String regex, String content, int groupIndex)
          获得匹配的字符串
static int getFirstNumber(String StringWithNumber)
          从字符串中获得第一个整数
static boolean isIpv4(String ip)
          判断该字符串是否是IPV4地址
static boolean isMatch(String regex, String content)
          给定内容是否匹配正则
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

NUMBER

public static final Pattern NUMBER

GROUP_VAR

public static final Pattern GROUP_VAR

IPV4

public static final Pattern IPV4
方法详细信息

get

public static String get(String regex,
                         String content,
                         int groupIndex)
获得匹配的字符串

参数:
regex - 匹配的正则
content - 被匹配的内容
groupIndex - 匹配正则的分组序号
返回:
匹配后得到的字符串,未匹配返回null

get

public static String get(Pattern pattern,
                         String content,
                         int groupIndex)
获得匹配的字符串

参数:
pattern - 编译后的正则模式
content - 被匹配的内容
groupIndex - 匹配正则的分组序号
返回:
匹配后得到的字符串,未匹配返回null

extractMulti

public static String extractMulti(Pattern pattern,
                                  String content,
                                  String template)
从content中匹配出多个值并根据template生成新的字符串
例如:
content 2013年5月 pattern (.*?})年(.*?)月 template: $1-$2 return 2013-5

参数:
pattern - 匹配正则
content - 被匹配的内容
template - 生成内容模板,变量 $1 表示group1的内容,以此类推
返回:
新字符串

extractMultiAndDelPre

public static String extractMultiAndDelPre(Pattern pattern,
                                           String[] contents,
                                           String template)
从content中匹配出多个值并根据template生成新的字符串
匹配结束后会删除匹配内容之前的内容(包括匹配内容)
例如:
content 2013年5月 pattern (.*?})年(.*?)月 template: $1-$2 return 2013-5

参数:
pattern - 匹配正则
contents - 被匹配的内容,数组0为内容正文
template - 生成内容模板,变量 $1 表示group1的内容,以此类推
返回:
新字符串

extractMulti

public static String extractMulti(String regex,
                                  String content,
                                  String template)
从content中匹配出多个值并根据template生成新的字符串
匹配结束后会删除匹配内容之前的内容(包括匹配内容)
例如:
content 2013年5月 pattern (.*?})年(.*?)月 template: $1-$2 return 2013-5

参数:
regex - 匹配正则字符串
content - 被匹配的内容
template - 生成内容模板,变量 $1 表示group1的内容,以此类推
返回:
按照template拼接后的字符串

extractMultiAndDelPre

public static String extractMultiAndDelPre(String regex,
                                           String[] contents,
                                           String template)
从content中匹配出多个值并根据template生成新的字符串
例如:
content 2013年5月 pattern (.*?})年(.*?)月 template: $1-$2 return 2013-5

参数:
regex - 匹配正则字符串
contents - 被匹配的内容
template - 生成内容模板,变量 $1 表示group1的内容,以此类推
返回:
按照template拼接后的字符串

delFirst

public static String delFirst(String regex,
                              String content)
删除匹配的内容

参数:
regex - 正则
content - 被匹配的内容
返回:
删除后剩余的内容

delPreLocation

public static String delPreLocation(String regex,
                                    String content)
删除正则匹配到的内容之前的字符 如果没有找到,则返回原文

参数:
regex - 定位正则
content - 被查找的内容
返回:
删除前缀后的新内容

findAll

public static <T extends Collection<String>> T findAll(String regex,
                                                       String content,
                                                       int group,
                                                       T collection)
取得内容中匹配的所有结果

参数:
regex - 正则
content - 被查找的内容
group - 正则的分组
collection - 返回的集合类型
返回:
结果集

findAll

public static <T extends Collection<String>> T findAll(Pattern pattern,
                                                       String content,
                                                       int group,
                                                       T collection)
取得内容中匹配的所有结果

参数:
pattern - 编译后的正则模式
content - 被查找的内容
group - 正则的分组
collection - 返回的集合类型
返回:
结果集

getFirstNumber

public static int getFirstNumber(String StringWithNumber)
从字符串中获得第一个整数

参数:
StringWithNumber - 带数字的字符串
返回:
整数

isIpv4

public static boolean isIpv4(String ip)
判断该字符串是否是IPV4地址

参数:
ip - IP地址
返回:
是否是IPV4

isMatch

public static boolean isMatch(String regex,
                              String content)
给定内容是否匹配正则

参数:
regex - 正则
content - 内容
返回:
正则为null或者""则不检查,返回true,内容为null返回false


Copyright © 2014. All rights reserved.