范文为教学中作为模范的文章,也常常用来指写作的模板。常常用于文秘写作的参考,也可以作为演讲材料编写前的参考。那么我们该如何写一篇较为完美的范文呢?接下来小编就给大家介绍一下优秀的范文该怎么写,我们一起来看一看吧。
帮助你驾驭java正则表达式 java正则表达式用法篇一
正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。java正则表达式和perl的是最为相似的。
java正则表达式的类在 包中,包括三个类:pattern,matcher 和 patternsyntaxexception。
pattern对象是正则表达式的已编译版本。他没有任何公共构造器,我们通过传递一个正则表达式参数给公共静态方法 compile 来创建一个pattern对象。
matcher是用来匹配输入字符串和创建的 pattern 对象的正则引擎对象。这个类没有任何公共构造器,我们用patten对象的matcher方法,使用输入字符串作为参数来获得一个matcher对象。然后使用matches方法,通过返回的布尔值判断输入字符串是否与正则匹配。
如果正则表达式语法不正确将抛出patternsyntaxexception异常。
让我们在一个简单的例子里看看这些类是怎么用的吧
package ; import .matcher;import .pattern; public class regexexamples { public static void main(string[] args) { // using pattern with flags pattern pattern = pattern pile("ab", _insensitive); matcher matcher = r("abcabdab"); // using matcher find(), group(), start() and end() methods while (()) { n("found the text "" + () + "" starting at " + () + " index and ending at index " + ()); } // using pattern split() method pattern = pattern pile("w"); string[] words = ("one@two#three:four$five"); for (string s : words) { n("split using (): " + s); } // using efirst() and replaceall() methods pattern = pattern pile("1*2"); matcher = r("11234512678"); n("using replaceall: " + eall("_")); n("using replacefirst: " + efirst("_")); } }
既然正则表达式总是和字符串有关, java 1.4对string类进行了扩展,提供了一个matches方法来匹配pattern。在方法内部使用pattern和matcher类来处理这些东西,但显然这样减少了代码的行数。
pattern类同样有matches方法,可以让正则和作为参数输入的字符串匹配,输出布尔值结果。
下述的代码可以将输入字符串和正则表达式进行匹配。
string str = "bbb";n("using string matches method: "+s(".bb"));n("using pattern matches method: "+s(".bb", str));
所以如果你的需要仅仅是检查输入字符串是否和pattern匹配,你可以通过调用string的matches方法省下时间。只有当你需要操作输入字符串或者重用pattern的时候,你才需要使用pattern和matches类。
注意由正则定义的pattern是从左至右应用的,一旦一个原字符在一次匹配中使用过了,将不会再次使用。
例如,正则“121”只会匹配两次字符串“31212142121″,就像这样“_121____121″。
java正则表达式元字符
有两种方法可以在正则表达式中像一般字符一样使用元字符。
在元字符前添加反斜杠()
将元字符置于q(开始引用)和e(结束引用)间
正则表达式量词
量词指定了字符匹配的发生次数。
量词可以和character classes和capturing group一起使用。
例如,[abc]+表示a,b或c出现一次或者多次。
(abc)+表示capturing group “abc”出现一次或多次。我们即将讨论capturing group。
正则表达式capturing group
capturing group是用来对付作为一个整体出现的多个字符。你可以通过使用()来建立一个group。输入字符串中和capturing group相匹配的部分将保存在内存里,并且可以通过使用backreference调用。
你可以使用count方法来获得一个正则pattern中capturing groups的.数目。例如((a)(bc))包含3个capturing groups; ((a)(bc)), (a) 和 (bc)。
你可以使用在正则表达式中使用backreference,一个反斜杠()接要调用的group号码。
capturing groups和backreferences可能很令人困惑,所以我们通过一个例子来理解。
n(s("(wd)1", "a2a2")); //true n(s("(wd)1", "a2b2")); //false n(s("(ab)(bd)21", "abb2b2ab")); //true n(s("(ab)(bd)21", "abb2b3ab")); //false
在第一个例子里,运行的时候第一个capturing group是(wd),在和输入字符串“a2a2″匹配的时候获取“a2″并保存到内存里。因此1是”a2”的引用,并且返回true。基于相同的原因,第二行代码打印false。
试着自己理解第三行和第四行代码。:)
我们可以创建一个带有标志的pattern对象。例如_insensitive可以进行大小写不敏感的匹配。pattern类同样提供了和string类相似的split(string) 方法
pattern类tostring()方法返回被编译成这个pattern的正则表达式字符串。
matcher类有start()和end()索引方法,他们可以显示从输入字符串中匹配到的准确位置。
matcher类同样提供了字符串操作方法replaceall(string replacement)和replacefirst(string replacement)。
现在我们在一个简单的java类中看看这些函数是怎么用的。
package ; import .matcher;import .pattern; public class regexexamples { public static void main(string[] args) { // using pattern with flags pattern pattern = pattern pile("ab", _insensitive); matcher matcher = r("abcabdab"); // using matcher find(), group(), start() and end() methods while (()) { n("found the text "" + () + "" starting at " + () + " index and ending at index " + ()); } // using pattern split() method pattern = pattern pile("w"); string[] words = ("one@two#three:four$five"); for (string s : words) { n("split using (): " + s); } // using efirst() and replaceall() methods pattern = pattern pile("1*2"); matcher = r("11234512678"); n("using replaceall: " + eall("_")); n("using replacefirst: " + efirst("_")); } }
found the text "ab" starting at 0 index and ending at index 2found the text "ab" starting at 3 index and ending at index 5found the text "ab" starting at 6 index and ending at index 8split using (): onesplit using (): twosplit using (): threesplit using (): foursplit using (): fiveusing replaceall: _345_678using replacefirst: _34512678
s("content_relate");
一键复制