类 RegularUtil

java.lang.Object
org.anyline.util.regular.RegularUtil

public class RegularUtil extends Object
  • 字段详细资料

  • 构造器详细资料

    • RegularUtil

      public RegularUtil()
  • 方法详细资料

    • match

      public static boolean match(String src, String regx, Regular.MATCH_MODE mode)
    • match

      public static boolean match(String src, String regx)
    • fetchs

      public static List<List<String>> fetchs(String src, String regx, Regular.MATCH_MODE mode) throws Exception
      提取子串
      参数:
      src - 输入字符串 src 输入字符串
      regx - 表达式 regx 表达式
      mode - mode
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • fetchs

      public static List<List<String>> fetchs(String src, String regx) throws Exception
      抛出:
      Exception
    • fetch

      public static List<String> fetch(String src, String regx) throws Exception
      抛出:
      Exception
    • fetch

      public static List<String> fetch(String src, String regx, Regular.MATCH_MODE mode, int idx) throws Exception
      抛出:
      Exception
    • fetch

      public static List<String> fetch(String src, String regx, Regular.MATCH_MODE mode) throws Exception
      抛出:
      Exception
    • filter

      public static List<String> filter(List<String> src, String regx, Regular.MATCH_MODE mode, Regular.FILTER_TYPE type)
    • pick

      public static List<String> pick(List<String> src, String regx, Regular.MATCH_MODE mode)
      过滤 保留匹配项
      参数:
      src - src
      regx - regx
      mode - mode
      返回:
      List
    • wipe

      public static List<String> wipe(List<String> src, String regx, Regular.MATCH_MODE mode)
      过滤 删除匹配项
      参数:
      src - src
      regx - regx
      mode - mode
      返回:
      List
    • indexOf

      public static int indexOf(String src, String regx, int begin)
      字符串下标 regx在src中首次出现的位置
      参数:
      src - src
      regx - regx
      begin - 有效开始位置
      返回:
      int
    • indexOf

      public static int indexOf(String src, String regx)
    • regexpValue

      public static List<String> regexpValue(String src, String regex, Regular.MATCH_MODE mode)
      表达式匹配值长度
      参数:
      src - src
      regex - regex
      mode - mode
      返回:
      List
    • removeTagWithBodyByAttribute

      public static String removeTagWithBodyByAttribute(String src, String attribute)
      删除所有 包含attribute属性 的标签 连同标签体一起删除
      RegularUtil.removeTagWithBodyByAttribute(str, "class")
      <input type="text" class="a"/>
      <input type="text" class="a"></input>
      <input type="text" class = "a"></input>
      <input type="text" class></input>
      <input type="text" class/>
      <input type="text" a="class"></input>(不匹配)
      参数:
      src - src
      attribute - 属性
      返回:
      String
    • removeTagWithBodyByAttributeValue

      public static String removeTagWithBodyByAttributeValue(String src, String attribute, String value)
      删除所有 包含attribute属性=value值 的标签连同标签体一起删除
      RegularUtil.removeTagWithBodyByAttributeValue(s, "class","a")
      <input type="text" class="a"/>
      <input type="text" class="a"/></input/>
      <input type="text" class="a b"/></input/>如果需要不匹配可以使用"[^\\s]a[^\\s]"
      <input type="text" class="b a"/></input/>
      <input type="text" class="ab"/></input/>(不匹配)如果需要匹配可以使用"a.*"
      参数:
      src - xml/html
      attribute - 属性
      value - 值
      返回:
      String
    • removeTagByAttributeValue

      public static String removeTagByAttributeValue(String src, String attribute, String value) throws Exception
      根据属性名与属性值 删除标签(只删除标签, 保留标签体)
      参数:
      src - xml/html
      attribute - 属性名
      value - 属性值
      返回:
      String
      抛出:
      Exception
    • removeTagByAttribute

      public static String removeTagByAttribute(String src, String attribute) throws Exception
      根据属性名 删除标签(只删除标签, 保留标签体)
      参数:
      src - xml/html
      attribute - 属性名
      返回:
      String
      抛出:
      Exception
    • getTagWithBodyByAttribute

      public static List<List<String>> getTagWithBodyByAttribute(String src, String attribute) throws Exception
      获取所有 包含attribute属性 的标签与标签体, 不支持相同标签嵌套
      [
      [整个标签含标签体, 开始标签, 结束标签, 标签体, 标签名称],
      [整个标签含标签体, 开始标签, 结束标签, 标签体, 标签名称]
      ]
      参数:
      src - xml/html
      attribute - 属性
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • getTagWithBodyByAttributeValue

      public static List<List<String>> getTagWithBodyByAttributeValue(String src, String attribute, String value) throws Exception
      获取所有 包含attribute属性并且值=value 的标签与标签体
      单标签只匹配有/>结尾的情况, 避免与双标签的开始标签混淆
      如class="a" : attribute=class value=a
      style="width:100px;" :attribute=style value=width
      [
      [整个标签含标签体, 开始标签, 结束标签, 标签体, 标签名称],
      [整个标签含标签体, 开始标签, 结束标签, 标签体, 标签名称]
      ]
      参数:
      src - xml/html
      attribute - 属性
      value - 值
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • removeTagExcept

      public static String removeTagExcept(String src, String... tags)
      删除 tags之外的标签"<b>"与"</b>"只写一次 "b"
      只删除标签不删除标签体
      参数:
      src - html
      tags - tags
      返回:
      String
    • removeHtmlTagExcept

      public static String removeHtmlTagExcept(String src, String... tags)
    • removeTag

      public static String removeTag(String src, String... tags)
      清除所有标签(只清除标签, 不清除标签体)
      参数:
      src - xml/html
      tags - tags
      返回:
      String
    • removeHtmlTag

      public static String removeHtmlTag(String src, String... tags)
    • removeTagWithBody

      public static String removeTagWithBody(String src, String... tags)
      删除标签及标签体
      参数:
      src - xml/html
      tags - 标签, 如果不提供则删除所有标签
      返回:
      String
    • removeHtmlTagWithBody

      public static String removeHtmlTagWithBody(String src, String... tags)
    • removeEmptyTag

      public static String removeEmptyTag(String src)
      删除所有空标签
      参数:
      src - xml/html
      返回:
      String
    • removeHtmlEmptyTag

      public static String removeHtmlEmptyTag(String src)
    • removeHtmlTagExceptSimple

      public static String removeHtmlTagExceptSimple(String src)
      删除简单标签外的其他标签
      参数:
      src - html
      返回:
      String
    • removeAttribute

      public static String removeAttribute(String src, String... attributes)
      删除所有标签的属性 只删除属性 不删除标签与标签体
      参数:
      src - xml/html
      attributes - 属性 如果不传则删除所有属性
      返回:
      String
    • fetchUrls

      public static List<String> fetchUrls(String src) throws Exception
      提取所有a棱中的url
      参数:
      src - xml/html
      返回:
      list
      抛出:
      Exception - 异常
    • fetchUrl

      public static String fetchUrl(String src) throws Exception
      抛出:
      Exception
    • fetchNumbers

      public static List<String> fetchNumbers(String src) throws Exception
      抛出:
      Exception
    • fetchNumber

      public static String fetchNumber(String src) throws Exception
      抛出:
      Exception
    • fetchPairedTag

      public static List<List<String>> fetchPairedTag(String txt, String... tags) throws Exception
      提取双标签<div>content<div> 依次取出p, table, div中的内容 有嵌套时只取外层 只能提取同时有 开始结束标签的内容, 不能提取单标签内容如<img> <br/> 支持不同标签嵌套, 但不支持相同标签嵌套 不区分大小写 0:全文 1:开始标签 2:标签name 3:标签体 4:结束标签
      参数:
      txt - text
      tags - 标签名, 如div, span tags标签名, 如div, span
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • fetchSingleTag

      public static List<List<String>> fetchSingleTag(String txt, String... tags) throws Exception
      提取单标签 如<img> <br/> 如果传入div等带有结束标签的参数 则只取出开始标签 <div> 不区分大小写 0:全文 1::标签name
      参数:
      txt - text
      tags - 标签名, 如img br
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • fetchAllTag

      public static List<List<String>> fetchAllTag(String txt, String... tags) throws Exception
      提取单标签+双标签 不区分大小写 0:全文 1:开始标签 2:标签name 3:标签体 (单标签时null) 4:结束标签 (单标签时null)
      参数:
      txt - txt
      tags - 标签名 tags标签名
      返回:
      List
      抛出:
      Exception - 异常 Exception
    • fetchAttributeList

      public static List<List<String>> fetchAttributeList(String txt, String attribute)
      取出所有属性值 0全文 1:属性name 2:引号('|") 3:属性值 fetchAttributeValues(txt, "id");
      参数:
      txt - txt
      attribute - 属性
      返回:
      List
    • fetchAttribute

      public static List<String> fetchAttribute(String txt, String attribute)
      取出属性及属性值 0全文 1:属性name 2:引号('|") 3:属性值 fetchAttributeValues(txt, "id","name");
      参数:
      txt - txt
      attribute - 属性
      返回:
      List
    • fetchAttributeValues

      public static List<String> fetchAttributeValues(String txt, String attribute)
      取出所有的属性值
      参数:
      txt - txt
      attribute - 属性名
      返回:
      List
    • fetchAttributeValue

      public static String fetchAttributeValue(String txt, String attribute)
      取出的属性值(有多个的取第一个)
      参数:
      txt - txt
      attribute - 属性名
      返回:
      String
    • cut

      public static String cut(String text, String... tags)
    • cut

      public static String cut(String text, boolean contains, String... tags)
      取tags[i-2]与tags[i-1]之间的文本
      参数:
      text - text
      contains - 是否包含开始结束标签
      tags - tags
      返回:
      String
    • cuts

      public static List<String> cuts(String text, String... tags)
    • cuts

      public static List<String> cuts(String text, boolean contains, String... tags)
    • isDate

      public static boolean isDate(String str)
    • isDateTime

      public static boolean isDateTime(String str)
    • isUrl

      public static boolean isUrl(String src)