`
wangangie12
  • 浏览: 45539 次
  • 性别: Icon_minigender_2
  • 来自: 厦门
最近访客 更多访客>>
社区版块
存档分类
最新评论

java的正则表达式 (基于官方帮助文档做部分说明)

阅读更多

  [/b][b]下面这段代码用了log4j,去下个log4j的包引入一下.测试的代码写在代码的 main函数里. import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; publicclass HtmlRegExtraction { static Logger logger = Logger.getLogger(HtmlRegExtraction.class); publicvoid writeHtmltoLocalFile(String localOutPutAdress, String html) { if (localOutPutAdress == null) { localOutPutAdress = "c:\\html.txt"; } FileOutputStream fos = null; try { fos = new FileOutputStream(localOutPutAdress); fos.write(html.getBytes()); System.out.println("html output location:" + localOutPutAdress); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @param regString * 正则表达式 * @param html * 输入的需要正则表达式匹配的字条串 * @param index * 取得哪一组的结果 * @return */public ArrayList parseHtml(String regString, String html, int index) { ArrayList resultHtmlArray = new ArrayList(); Pattern p = Pattern.compile(regString); Matcher m = p.matcher(html); // 全文中找到的配数int count = 0; logger.debug("当前 正则表达式可拆分为在:" + m.groupCount() + "组(0组为最大捕获组)"); while (m.find()) { count++; logger.debug("第"+(count)+"条匹配纪录"); for (int i = 0; i 正则表达式的记录 "); return resultHtmlArray; } publicstaticvoid main(String[] args) { HtmlRegExtraction hre = new HtmlRegExtraction(); String ret = "String written in here"; hre.parseHtml("regularation written in here
  [/b][b][/b][b]写在最开头,什么叫正则表达式.正则表达式就是一把锁.对应的字符串就是钥匙,只有当钥匙完全符合这把锁才以打开.也就是所谓的匹配.
  
[/b][b]写正则表达式就是做做一把锁.去找所有能打开它的钥匙.
  
[/b][b]看这篇文章的前提是你看了
  
正则表达式30分钟入门教程 GOOGLE一下吧..
  
[/b][b]java.util.regex.Pattern
  
A compiled representation of a regular expression. 
  A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern. 
  A typical invocation sequence is thus 
  Pattern p = Pattern. compile("a*b"); 
  Matcher m = p. matcher("aaaaab"); 
  boolean b = m. matches(); 
  A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an input sequence against it in a single invocation. The statement 
  boolean b = Pattern.matches("a*b", "aaaaab"); 
  is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused. 
  Instances of this class are immutable and are safe for use by multiple concurrent threads. Instances of the Matcher class are not safe for such use. 
  Summary of regular-expression constructs  Construct Matches [/b][b]Characters x The character x \\ The backslash character \0n The character with octal value 0n (0 正则能不用则少用~
  [/b][^\\p{ASCII}0-9]+
  
正确的写法居然是这样..
  
[^\\p{ASCII}[0-9]]+
  
[/b][b]\p{Alpha} An alphabetic character:[\p{Lower}\p{Upper}] \p{Digit} A decimal digit: [0-9] \p{Alnum} An alphanumeric character:[\p{Alpha}\p{Digit}] \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;?@[\]^_`{|}~ \p{Graph} A visible character: [\p{Alnum}\p{Punct}] \p{Print} A printable character: [\p{Graph}\x20] \p{Blank} A space or a tab: [ \t] \p{Cntrl} A control character: [\x00-\x1F\x7F] \p{XDigit} A hexadecimal digit: [0-9a-fA-F] \p{Space} A whitespace character: [ \t\n\x0B\f\r] [/b][b]java.lang.Character classes (simple java character type) \p{javaLowerCase} Equivalent to java.lang.Character.isLowerCase() \p{javaUpperCase} Equivalent to java.lang.Character.isUpperCase() \p{javaWhitespace} Equivalent to java.lang.Character.isWhitespace() \p{javaMirrored} Equivalent to java.lang.Character.isMirrored() [/b][b]Classes for Unicode blocks and categories 要用上这个里面的东西.首先你得知道什么叫希腊字母.GOOGLE之. \p{InGreek} A character in the Greek block (simple block) \p{Lu} An uppercase letter (simple category) \p{Sc} A currency symbol我觉得这个还有点用(捕获货币符号) \P{InGreek} Any character except one in the Greek block (negation) [\p{L}&&[^\p{Lu}]]  Any letter except an uppercase letter (subtraction) [/b][b]你认为[\\p{L}]*能捕获字符串”123a我$ ”捕到什么,只能捕获到字母与中文.且捕获到5条符合的记录.
  
分别是
  
[/b][b][/b][b]a
  
[/b][b]不是说5条纪录么!!!怎么就一个”a我”!!..因为其他捕获到的组只是做为标识用了..具体过程如下:
  
正则引擎先拿到1,一看:是个数字,匹配了标识(以数字做标识,但不捕获)再找下一个.又是一数字..完全不匹配.这样如果把[\\p{L}]*用标准的正则表达式写应该是这样(?正则表达式.写在(?= )内.只是一个标识.表示我只捕获符合以X正则表达式结尾的字符串. 
  例如a.*(?=b) 
  传入adcbac 
  将会捕获adc c后面跟了一个b,这告诉看到正则表达式看到b就停.但不捕获它. 
  以下的参数意义上不同.但原理一样. (?!X) X, via zero-width negative lookahead  只捕获不符合以X正则表达式结尾的字符串. (?正则表达式开头的字符串. (?正则表达式开头的字符串. (?>X) X, as an independent, non-capturing group  这个要跟(?:X) 对比一下 
  (?:X)与(?>X)最大的区别在于前者捕获,后者不捕获. 
  举例: 
  待捕获字符串:abcde 
  用(?>a)(bc)将捕获 bc,此时!这个正则只捕获到1组group,只有(bc)!! [b]Backslashes, escapes, and quoting 
The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.  It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.  Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.  Character Classes  Character classes may appear within other character classes, and may be composed by the union operator (implicit) and the intersection operator (&&). The union operator denotes a class that contains every character that is in at least one of its operand classes. The intersection operator denotes a class that contains every character that is in both of its operand classes.  The precedence of character-class operators is as follows, from highest to lowest:  [/b][b][/b][b][/b]Note that a different set of metacharacters are in effect inside a character class than outside a character class. For instance, the regular expression . loses its special meaning inside a character class, while the expression - becomes a range forming metacharacter.  [b]Line terminators  A line terminator is a one- or two-character sequence that marks the end of a line of the input character sequence. The following are recognized as line terminators: 
分享到:
评论

相关推荐

    JAVA 正则表达式 教程

    由于当前版本的 Java Tutorial 是基于 JDK 6.0 的,因此其中的示例程序也用到了 JDK 6.0 中的新增类库,但正则表达式在 JDK 1.4 就已经存在了,为了方便大家使用,改写了部分的源代码,源代码类名中后缀为"V4"的表示...

    Java正则表达式详解+基于HTMLParser解析HTML网页

    如何在Java程序中利用正则表达式实现对字符串的解析.另外,HTMLParser是一款很强大的对HTML网页进行解析的工具,其中大量地用到正则表达式.

    正则表达式

    一个基于java的正则表达式文档,让你对正则表达式不再心有余悸.......

    Java基于正则表达式实现查找匹配的文本功能【经典实例】

    主要介绍了Java基于正则表达式实现查找匹配的文本功能,结合具体实例形式分析了java正则查找、字符串遍历、group分组相关操作技巧,需要的朋友可以参考下

    词法分析程序生成器实现将正则表达式、NFA、DFA、DFA最小化词法分析程序.zip

    词法分析程序生成器实现将正则表达式、NFA、DFA、DFA最小化词法分析程序 词法分析程序生成器是一个工具,它能够根据给定的正则表达式自动生成词法分析器。这个生成器通常包括以下几个步骤:正则表达式的转换、NFA...

    jsp+javascript+html+正则表达式+mysql+spring+css+strtus技术文档

    齐全的查询手册,如果有朋友需要可以留言给我,因为大小有限,暂时先上传一部分,大家看看用的着不。

    JAVA_API1.6文档(中文)

    java.util.regex 用于匹配字符序列与正则表达式指定模式的类。 java.util.spi java.util 包中类的服务提供者类。 java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类。 javax.accessibility 定义了用户...

    Java基础文档

    第5章 、 字符串和正则表达式 15 第6章 、 常用的实用类 17 第7章 、 线程 17 第8章 、 输入\输出流 21 第9章 、 基于SWING的图形用户界面的设计 21 第10章 、 Java中的网络编程 21 第11章 、 Java与数据库中的操作 ...

    java基础学习总结笔记

    知识主要包括:Java基础常识、如何安装Java工具、Java语言的基础组成、Java面向对象、Java多线程、Java常用类、集合(重点)、IO流、GUI图形界面、网络编程、正则表达式、反射、注解、类加载器、动态代理等等,另外...

    Jmeter-Java接口自动化学习文档.rar

    Apache JMeter是开源软件,它可以用来做负载测试和性能测试。它最初设计是用来测试Web应用程序,但现已扩展到其他测试功能。 Apache JMeter可以用来测试...为了最大的灵活性,JMeter允许我们使用正则表达式创建断言。

    JAVA高并发高性能高可用高扩展架构视频教程

    网络爬虫之JAVA正则表达式 手写springMVC框架 老司机带你透析springMVC内部实现方式 打造高效代码结构(java性能优化) 新版本通俗易懂_观察者模式递进时讲解 ibatis连接数据库 高并发之单(多)生产者消费者线程 高并发...

    JavaAPI1.6中文chm文档 part1

    java.util.regex 用于匹配字符序列与正则表达式指定模式的类。 java.util.spi java.util 包中类的服务提供者类。 java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类。 javax.accessibility 定义了用户...

    java项目源码之UrlRewriter Java v2.0 RC1_urlrewriterjava.rar

    正则表达式支持:支持使用正则表达式匹配和转换 URL,实现更加复杂和灵活的 URL 重写功能。 多种重写策略:提供多种 URL 重写策略,包括基于规则匹配、请求参数匹配、路径匹配等,满足不同场景下的 URL 重写需求。 ...

    editplus 代码编辑器html c++ jsp css

    具体解决方法,在 Editplus 中使用正则表达式,由于“(”、“)”被用做预设表达式(或者可以称作子表达式)的标志,所以查找 “ \n” 时会提示查找不到,所以也就无法进行替换了,这时可以把“(”、“)”使用任意...

    javajava概要设计方案.doc

    < thinking in java >(JNI) < java核心技术第2卷> (JNI)正则表达式 2.总体设计 2.1需求规定 2.1.1系统功能 本系统功能定位为为用户提供网页搜索功能,通过简单的提交关键字,实现页面检索 2.1.2系统性能 索引...

    流数据多模式匹配库(含源代码)

    流数据的正则表达式库是一个在数据流中进行正则匹配和搜索的工具,支持多模式匹配,通常运用在自动人机交互、网络通讯、大文本搜索等需要对流数据进行处理的应用中,如交换机自动操作程序、终端的命令自动执行,适用...

    EditPlus 2整理信箱的工具

    这个也算正则表达式的用法,其实仔细看正则表达式应该比较简单,不过既然有这个问题提出,说明对正则表达式还得有个认识过程,解决方法如下 解决: 在替换对话框中,启用“正则表达式”复选框 在查找内容里面输入...

    基于Java实现的简单的词法分析器和语法分析器.zip

    基于Java实现的简单的词法分析器和语法分析器是一个基础的编译器前端项目,旨在帮助开发者理解编译器的前端部分是如何工作的。以下是一个简化的项目介绍,描述了如何使用Java实现这两个分析器。 ### 项目介绍: **...

    Editplus 3[1].0

    这个也算正则表达式的用法,其实仔细看正则表达式应该比较简单,不过既然有这个问题提出,说明对正则表达式还得有个认识过程,解决方法如下 解决: 在替换对话框中,启用“正则表达式”复选框 在查找内容里面输入...

Global site tag (gtag.js) - Google Analytics