`
george.gu
  • 浏览: 71176 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java Regular Expression (Java正则表达式)

阅读更多

In current Project, we need to parse log file (written by log4j) and extract dedicated information for future actions:

 

  1. Display info in GUI
  2. Analyze error and send mail to responder
  3. Backup key logs into DB which can be shared to external system
  4. ...
Oh, what's the topic I am going to talk in this blog? ERP, CRM or requirement for DMS? OK, that's Reguler Expression in Java. Forgive me, sometime enginers start talking in topic A but finally they made a deal for topic B. 
So Regular Expression is one solution to parse some log files.

I will talk about general regular expression semantics first and then some regular expression usage with java.

Regular Expression Semantics

Common Match Sysmbols

RE

Description

.

Matches any sign

^regex

regex must match at the beginning of the line

regex$

Finds regex must match at the end of the line

[abc]

Set definition, can match the letter a or b or c

[abc[vz]]

Set definition, can match a or b or c followed by either v or z

[^abc]

When a "^" appears as the first character inside [] when it negates the pattern. This can match any character except a or b or c

[a-d1-7]

Ranges, letter between a and d and figures from 1 to 7, will not match d1

X|Z

Finds X or Z

XZ

Finds X directly followed by Z

$

Checks if a line end follows

 

Special Metacharacters

RE

Description

\d

Any digit, short for [0-9]

\D

A non-digit, short for [^0-9]

\s

A whitespace character, short for [ \t\n\x0b\r\f]

\S

A non-whitespace character, for short for [^\s]

\w

A word character, short for [a-zA-Z_0-9]

\W

A non-word character [^\w]

\S+

Several non-whitespace characters

 

Quantifier

RE

Description

Examples

*

Occurs zero or more times, is short for {0,}

X* - Finds no or several letter X, .* - any character sequence

+

Occurs one or more times, is short for {1,}

X+ - Finds one or several letter X

?

Occurs no or one times, ? is short for {0,1}

X? -Finds no or exactly one letter X

{X}

Occurs X number of times, {} describes the order of the preceding liberal

\d{3} - Three digits, .{10} - any character sequence of length 10

{X,Y}

.Occurs between X and Y times,

\d{1,4}- \d must occur at least once and at a maximum of four

*?

? after a qualifier makes it a "reluctant quantifier", it tries to find the smallest match.

 

 

Java Regular Expression Usage

To be updated.

分享到:
评论

相关推荐

    常用java正则表达式

    如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。 许多语言,包括Perl、PHP、Python、JavaScript和JScript,都支持用正则表达式处理...

    java常用正则表达式 和 java 正则表达式详解 csdn 下载

    如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式...如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。

    java正则表达式.docx

    java正则表达式.docx 如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符...

    Java正则表达式详解

    如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。 许多语言,包括Perl、PHP、Python、JavaScript和JScript,都支持用正则表达式...

    java正则表达式: regular expression(一)

    本文实例源码 博文链接:https://gmf.iteye.com/blog/89077

    Mastering Regular Expressions 3 ed pdf 版 精通正则表达式(第三版)

    《精通正则表达式》是系统学习正则表达式的唯一最权威著作。任何时候,任何地方,只要提到正则表达式著作,人们都会提到这本书。该书质量之高,声誉之盛,使得几乎没有人企图挑战它的地位,从而在正则表达式图书领域...

    Java 正则表达式库

    一个非常完备,快速的Java正则表达式库,有详细的说明文档

    java基础09-正则表达式.pptx

    正则表达式,又称规则...当然在Java中也可以通过处理字符串的方式达到检索,替换文本字符串的目的,但是有了正则表达式写代码更加简洁,通常两三行代码就可以达到目的,当然这也是建立在熟悉正则表达式的基础之上的。

    java中常用的正则式

    正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。 例如: runoo+b,可以匹配 runoob、...

    PHP正则表达式基本语法和使用方法

    正则表达式(regular expression)是一种表示方式,最早在LINUX被当做一种搜索算法应用在编辑器中,后来被广泛应用,不仅PHP脚本支持正则表达式,Perl、C#、Java以及JavaScript和MySQL也对正则表达式支持。...

    正则表达式手册,Regular Expression

    一个轻量级的正则表达式手册,对做Javascript开发, 或者使用JAVA做字符串的操作非常有用。

    正则表达式袖珍手册

    正则表达式袖珍手册(Regular Expression Pocket Reference: Regular Expressions for Perl, Ruby, PHP, Python, C, Java and .NET (Pocket Reference一书的译稿)

    Java 正则表达式详解

    如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。 正则表达式30分钟入门教程 常用正则表达式 许多语言,包括Perl、PHP、Python、...

    RegularExpressionTester:在网站上测试Java正则表达式

    这是Java正则表达式的测试工具支持在Google App Engine上运行。 它部署在下面的链接。 Java正则表达式测试器 用法 输入文字。 输入目标文本,正则表达式模式和替换(可选)。 点击“测试”按钮 单击“测试”按钮...

Global site tag (gtag.js) - Google Analytics