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正则表达式的解释说明 表达式意义: 1.字符 x    字符 x。例如a表示字符a \\    反斜线字符。在书写时要写为\\\\。(注意:因为java在第一次解析时,把\\\\解析成...
  • qq_22118507
  • qq_22118507
  • 2016年06月14日 19:57
  • 4191

java正则表达式语法详解及其使用代码实例

java正则表达式语法详解及其使用代码实例 http://www.zuidaima.com/share/1835085544524800.htm...
  • yaerfeng
  • yaerfeng
  • 2014年06月06日 09:32
  • 66126

java正则表达式入门及API实例

一、正则表达式语法 二、java API及实例
  • LinBilin_
  • LinBilin_
  • 2016年11月28日 10:22
  • 1308

正则表达式知识示例详解源代码(java版)

  • 2016年04月27日 12:37
  • 20KB
  • 下载

Java正则表达式教程及示例

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

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

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

Java正则表达式教程及示例

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

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

正则表达式知识详解系列,通过代码示例来说明正则表达式知识  源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 示例功能: ...
  • gnail_oug
  • gnail_oug
  • 2016年04月29日 09:23
  • 306

Java正则表达式教程及示例

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

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

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

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