正则表达式

基本语法

+ :其前导字符必须在目标对象中连续出现一次或多次。

* 其前导字符必须在目标对象中出现零次或连续多次。

? 其前导对象必须在目标对象中连续出现零次或一次。

{n} n 是一个非负整数。匹配确定的 n 次。

{n,} n 是一个非负整数。至少匹配 n 次。

{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。

\s:用于匹配单个空格符,包括tab键和换行符;

\S:用于匹配除单个空格符之外的所有字符;

\d:用于匹配从0到9的数字;

\D:匹配一个非数字字符。等价于 [^0-9]。

\w:用于匹配字母,数字或下划线字符;

\W:用于匹配所有与\w不匹配的字符;

\f 匹配一个换页符。

\n 匹配一个换行符。

\r 匹配一个回车符。

\t 匹配一个制表符。

\v 匹配一个垂直制表符。

\xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例:'\x41' 匹配 "A"。

\num 匹配 num,其中 num 是一个正整数。

. :用于匹配除换行符(\n)之外的所有字符。

^ 匹配模式必须出现在目标字符串的开头

$ 匹配模式必须出现在目标对象的结尾

\b 匹配模式必须出现在目标字符串的开头或结尾的两个边界之一

\B 匹配对象必须位于目标字符串的开头和结尾两个边界之内, 即匹配对象既不能作为目标字符串的开头,也不能作为目标字符串的结尾。

[A-Z] 与从A到Z范围内任何一个大写字母相匹配

[a-z] 与从a到z范围内任何一个小写字母相匹配。

[0-9] 与从0到9范围内任何一个数字相匹配。

(?:pattern) 例:industr(?:y|ies) 用来匹配industry或industries

(?=pattern) 正向预查。例:Windows (?=95|98|NT|2000) 能匹配 "Windows 2000" 中的 "Windows"

(?!pattern) 负向预查。例:Windows (?!95|98|NT|2000) 能匹配 "Windows 3.1" 中的 "Windows",不能匹配"Windows 2000" 中的 "Windows"  

([a-z][A-Z][0-9])+ 与任何由字母和数字组成的字符串,如 “aB0” 等相匹配。 无法与诸如 “abc”等的字符串匹配,因为“abc”中的最后一个字符为字母而非数字。 () 把字符串组合在一起。()符号包含的内容必须同时出现在目标对象中

| 如果我们希望在正则表达式中实现类似编程逻辑中的“或”运算,在多个不同的模式中任选一个进行匹配的话,可以使用管道符

^出现在 []内时就被视做否定运算符;而当^位于[]之外,或没有[]时,则应当被视做定位符。

当用户需要在正则表达式的模式中加入元字符,并查找其匹配对象时,可以使用转义符“\”

---------------------------------------------------------------------------------------------------------

-- JAVASCRIPT

---------------------------------------------------------------------------------------------------------

<html>

  <head>

    <script language="Javascript">

      function verifyAddress(obj) {

        var email = obj.email.value;

        var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

        flag = pattern.test(email);

        if(flag) {

          alert("Your email address is correct!");

          return true;

        } else {

          alert("Please try again!");

          return false;

        }

      }

    </script>

  </head>

  <body>

    <form onSubmit="return verifyAddress(this);">

      <input name="email" type="text">

      <input type="submit">

    </form>

  </body>

</html>

 

正则表达式对象

语法 1

re = /pattern/[flags]

语法 2

re = new RegExp("pattern",["flags"])

Flags : g (全文查找出现的所有 pattern)

        : i (忽略大小写)

        : m (多行查找)

---------------------------------------------------------------------------------------------------------

语法 1 的例子:

<html>

  <head>

    <script language="Javascript">

      function MatchDemo() {

        var str = "<html><head>aaa </head></html><p>bbb</p>";

        var regx = /<(.*)>.*<\/\1>/g;

        var re = str.match(regx);

        //var re = regx.exec(str);

        document.getElementById("str").value = re;

        return;

      }

    </script>

  </head>

  <body>

    <input id="str" type="text">

    <input value="click" type="button" onclick="MatchDemo();">

  </body>

</html>

---------------------------------------------------------------------------------------------------------

语法 2 的例子:

<html>

  <head>

    <script language="Javascript">

      function MatchDemo() {

        var str = "<html><head>aaa </head></html><p>bbb</p>";

        var regx = new RegExp("<(.*)>.*</\\1>","g");  // new RegExp()方法中需要注意‘\’字符的使用

        // var re = str.match(regx);

        var re = regx.exec(str);

        // re = regx.exec(str);

        document.getElementById("str").value = re;

        return;

      }

    </script>

  </head>

  <body>

    <input id="str" type="text">

    <input value="click" type="button" onclick="MatchDemo();">

  </body>

</html>

---------------------------------------------------------------------------------------------------------

如果 exec 方法或 match 方法没有找到匹配,则它返回 null。 如果它找到匹配,则 exec 方法或 match 方法返回一个数组,并且更新全局 RegExp 对象的属性,以反映匹配结果。 数组的0元素包含了完整的匹配,而第1到n元素中包含的是匹配中出现的任意一个子匹配。 如果为正则表达式设置了全局标志,exec或 match 从以 lastIndex 的值指示的位置开始查找。 如果没有设置全局标志,exec或 match 忽略 lastIndex 的值,从字符串的起始位置开始搜索。

match()非全局的正则表达式的话,执行结果和这个正则表达式的exec()方法执行结果一样。 exec 方法,match 方法返回的数组有三个属性,分别是 input、index 和 lastIndex。 Input 属性包含了整个被查找的字符串。 Index 属性中包含了整个被查找字符串中被匹配的子字符串的位置。 LastIndex 属性中包含了匹配中最后一个字符的下一个位置。

---------------------------------------------------------------------------------------------------------

test 方法

返回一个 Boolean 值,它指出在被查找的字符串中是否存在模式。

<html>

  <head>

    <script language="Javascript">

      function MatchDemo() {

        var str = "<html><head>aaa </head></html><p>bbb</p>";

        var regx = new RegExp("<(.*)>.*</\\1>","m");

        var flg = regx.test(str);

        document.getElementById("str").value = flg;

        return;

      }

    </script>

  </head>

  <body>

    <input id="str" type="text">

    <input value="click" type="button" onclick="MatchDemo();">

  </body>

</html>

---------------------------------------------------------------------------------------------------------

search 方法

返回与正则表达式查找内容匹配的第一个子字符串的位置。

<html>

  <head>

    <script language="Javascript">

      function MatchDemo() {

        var str = "<html><head>aaa </head></html><p>bbb</p>";

        var regx = new RegExp("<(.*)>.*</\\1>","m");

        var flg = str.search(regx);

        document.getElementById("str").value = flg;

        return;

      }

    </script>

  </head>

  <body>

    <input id="str" type="text">

    <input value="click" type="button" onclick="MatchDemo();">

  </body>

</html>

---------------------------------------------------------------------------------------------------------

-- JAVA

---------------------------------------------------------------------------------------------------------

Java java.util.regex

1). Java中在某个字符串中查询某个字符或者某个子字串

String str = "hello world!";

String regx = "or";

Pattern p = Pattern.compile(regx); // Pattern.compile(regx,Pattern.CASE_INSENSITIVE); 忽略大小写

Matcher m = p.matcher(str);

boolean rs = m.find();

 

2). 在某个文件中获取一段字符串

String str = "hello world!";

String regx = "or";

Pattern p = Pattern.compile(regx); // Pattern.compile(regx,Pattern.CASE_INSENSITIVE); 忽略大小写

Matcher m = p.matcher(str);

boolean rs = m.find();

for (int i=1; i<=m.groupCount(); i++) {

  System.out.println(m.group(i));

}

3). 字符串的替换/删除

String regEx="aaa+";

Pattern pat=Pattern.compile(regEx);

Matcher mat=pat.matcher("aaabbbccaadd");

String s=mat.replaceAll("#");

posted @ 2014-03-04 20:57  keyiei  阅读(239)  评论(0编辑  收藏  举报