Java正则表达式示例

转载 2015年07月10日 10:44:52


<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">import java.util.regex.Matcher;
import java.util.regex.Pattern;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> RegexMatches
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>( String args[] ){

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 按指定模式在字符串查找</span>
      String line = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This order was placed for QT3000! OK?"</span>;
      String pattern = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(.*)(\\d+)(.*)"</span>;

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建 Pattern 对象</span>
      Pattern r = Pattern.compile(pattern);

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 现在创建 matcher 对象</span>
      Matcher m = r.matcher(line);
      <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (m.find( )) {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) );
      } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"NO MATCH"</span>);
      }
   }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

主要步骤:

  1. 通过正则表达式构建Pattern对象。
  2. 通过Pattern构建Matcher对象
  3. 调用Matcher.find()获得匹配结果
转载自:
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">import java.util.regex.Matcher;
import java.util.regex.Pattern;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> RegexMatches
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>( String args[] ){

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 按指定模式在字符串查找</span>
      String line = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This order was placed for QT3000! OK?"</span>;
      String pattern = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(.*)(\\d+)(.*)"</span>;

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建 Pattern 对象</span>
      Pattern r = Pattern.compile(pattern);

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 现在创建 matcher 对象</span>
      Matcher m = r.matcher(line);
      <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (m.find( )) {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) );
      } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"NO MATCH"</span>);
      }
   }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

主要步骤:

  1. 通过正则表达式构建Pattern对象。
  2. 通过Pattern构建Matcher对象
  3. 调用Matcher.find()获得匹配结果

转载自:http://blog.csdn.net/u012956540/article/details/46797399
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">import java.util.regex.Matcher;
import java.util.regex.Pattern;

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> RegexMatches
{
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">main</span>( String args[] ){

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 按指定模式在字符串查找</span>
      String line = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This order was placed for QT3000! OK?"</span>;
      String pattern = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(.*)(\\d+)(.*)"</span>;

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建 Pattern 对象</span>
      Pattern r = Pattern.compile(pattern);

      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 现在创建 matcher 对象</span>
      Matcher m = r.matcher(line);
      <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (m.find( )) {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) );
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Found value: "</span> + m.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>) );
      } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
         System.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">out</span>.println(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"NO MATCH"</span>);
      }
   }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li></ul>

主要步骤:

  1. 通过正则表达式构建Pattern对象。
  2. 通过Pattern构建Matcher对象
  3. 调用Matcher.find()获得匹配结果

相关文章推荐

Java正则表达式教程及示例

当我开始我的Java职业生涯的时候,对于我来说正则表达式简直是个是梦魇。本教程旨在帮助你驾驭Java正则表达式,同时也帮助我复习正则表达式。 什么是正则表达式? 正则表达式定义了字符串的模式。正则...

正则表达式知识详解之贪婪模式和非贪婪模式 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识,建议自己按照例子手打一遍。 本示例的源代码下载地址:http://download.csdn.net/detail/gnail_oug/95...

正则表达式知识详解之替换 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识  源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 示例功能: ...

Java正则表达式教程及示例

当我开始我的Java职业生涯的时候,对于我来说正则表达式简直是个是梦魇。本教程旨在帮助你驾驭Java正则表达式,同时也帮助我复习正则表达式。 什么是正则表达式? 正则表达式定义了字符串的模...

正则表达式知识详解之实战 读取网页中超链接 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识  源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 ...

正则表达式知识详解之前后查找(环视) (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识  源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 示例功能...

正则表达式知识详解之基础知识 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识,建议自己按照例子手打一遍。 本示例的源代码下载地址: /** * 正则表达式基础 * @date 2016-04-15 10...

正则表达式知识详解之回溯引用 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识  源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 示例功能: ...

正则表达式知识详解之单词边界 (java版示例)

正则表达式知识详解系列,通过代码示例来说明正则表达式知识,源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094String str="th...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java正则表达式示例
举报原因:
原因补充:

(最多只允许输入30个字)