您好,欢迎来电子发烧友网! ,新用户?[免费注册]

您的位置:电子发烧友网>源码下载>java源码下载>

关于java正则表达式的用法详解

大小:1.4 MB 人气: 2017-09-27 需要积分:2

   正则表达式

  一个正则表达式是一个用于文本搜索的文本模式。换句话说,在文本中搜索出现的模式。例如,你可以用正则表达式搜索网页中的邮箱地址或超链接。

  正则表达式示例

  下面是一个简单的Java正则表达式的例子,用于在文本中搜索 http://

  Stringtext= “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;Stringpattern = “.*http://.*”; booleanmatches = Pattern.matches(pattern, text); System.out.println( “matches = ”+ matches);

  示例代码实际上没有检测找到的 http:// 是否是一个合法超链接的一部分,如包含域名和后缀(.com,.net 等等)。代码只是简单的查找字符串 http:// 是否出现。

  Java6 中关于正则表达式的API

  本教程介绍了Java6 中关于正则表达式的API。

  Pattern (java.util.regex.Pattern)

  类 java.util.regex.Pattern 简称 Pattern, 是Java正则表达式API中的主要入口,无论何时,需要使用正则表达式,从Pattern 类开始

  Pattern.matches()

  检查一个正则表达式的模式是否匹配一段文本的最直接方法是调用静态方法Pattern.matches(),示例如下:

  Stringtext= “This is the text to be searched ”+ “for occurrences of the pattern.”;Stringpattern = “.*is.*”; booleanmatches = Pattern.matches(pattern, text); System.out.println( “matches = ”+ matches);

  上面代码在变量 text 中查找单词 “is” 是否出现,允许”is” 前后包含 0或多个字符(由 .* 指定)

  Pattern.matches() 方法适用于检查 一个模式在一个文本中出现一次的情况,或适用于Pattern类的默认设置。

  如果需要匹配多次出现,甚至输出不同的匹配文本,或者只是需要非默认设置。需要通过Pattern.compile() 方法得到一个Pattern 实例。

  Pattern.compile()

  如果需要匹配一个正则表达式在文本中多次出现,需要通过Pattern.compile() 方法创建一个Pattern对象。示例如下

  Stringtext = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Patternpattern = Pattern.compile(patternString);

  可以在Compile 方法中,指定一个特殊标志:

  Patternpattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

  Pattern 类包含多个标志(int 类型),这些标志可以控制Pattern 匹配模式的方式。上面代码中的标志使模式匹配是忽略大小写

  Pattern.matcher()

  一旦获得了Pattern对象,接着可以获得Matcher对象。Matcher 示例用于匹配文本中的模式。示例如下

  Matcher matcher = pattern.matcher(text);

  Matcher类有一个matches()方法,可以检查文本是否匹配模式。以下是关于Matcher的一个完整例子

  String text = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;String patternString = “.*http://.*”;Pattern pattern = Pattern .compile(patternString, Pattern .CASE_INSENSITIVE) ;Matcher matcher = pattern .matcher(text) ;boolean matches = matcher .matches() ;System .out.println( “matches = ”+ matches) ;

  Pattern.split()

  Pattern 类的 split()方法,可以用正则表达式作为分隔符,把文本分割为String类型的数组。示例:

  Stringtext = “A sep Text sep With sep Many sep Separators”; StringpatternString = “sep”; Pattern pattern = Pattern.compile(patternString); String[] split= pattern. split(text); System.out.println( “split.length = ”+ split.length); for( Stringelement : split){ System.out.println( “element = ”+ element); }

  上例中把text 文本分割为一个包含5个字符串的数组。

  Pattern.pattern()

  Pattern 类的 pattern 返回用于创建Pattern 对象的正则表达式,示例:

  StringpatternString = “sep”; Patternpattern = Pattern.compile(patternString);Stringpattern2 = pattern.pattern();

  上面代码中 pattern2 值为sep ,与patternString 变量相同。

  Matcher (java.util.regex.Matcher)

  java.util.regex.Matcher 类用于匹配一段文本中多次出现一个正则表达式,Matcher 也适用于多文本中匹配同一个正则表达式。

  Matcher 有很多有用的方法,详细请参考官方JavaDoc。这里只介绍核心方法。

  以下代码演示如何使用Matcher

  Stringtext= “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher( text); booleanmatches = matcher.matches();

  首先创建一个Pattern,然后得到Matcher ,调用matches() 方法,返回true 表示模式匹配,返回false表示不匹配。

  可以用Matcher 做更多的事。

  创建Matcher

  通过Pattern 的matcher() 方法创建一个Matcher。

  Stringtext = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;StringpatternString = “.*http://.*”; Patternpattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(text);

  matches()

  Matcher 类的 matches() 方法用于在文本中匹配正则表达式

  boolean matches= matcher. matches();

  如果文本匹配正则表达式,matches() 方法返回true。否则返回false。

  matches() 方法不能用于查找正则表达式多次出现。如果需要,请使用find(), start() 和 end() 方法。

  lookingAt()

  lookingAt() 与matches() 方法类似,最大的不同是,lookingAt()方法对文本的开头匹配正则表达式;而

  matches() 对整个文本匹配正则表达式。换句话说,如果正则表达式匹配文本开头而不匹配整个文本,lookingAt() 返回true,而matches() 返回false。 示例:

  String text = “This is the text to be searched ”+ “for occurrences of the http:// pattern.”;String patternString = “This is the”;Pattern pattern = Pattern.compile(patternString, Pattern .CASE_INSENSITIVE) ;Matcher matcher = pattern.matcher(text) ;System .out.println( “lookingAt = ”+ matcher .lookingAt()) ;System.out.println( “matches = ”+ matcher .matches()) ;

非常好我支持^.^

(0) 0%

不好我反对

(0) 0%

      发表评论

      用户评论
      评价:好评中评差评

      发表评论,获取积分! 请遵守相关规定!