日期:2014-05-19  浏览次数:20653 次

求一个正则表达式。另外求正则表达式学习资料
Java code
      public static final int openedHandle=0x7f010007;
        /**  Defines panel position on the screen. 
         <p>Must be one of the following constant values.</p>
<table>
<colgroup align="left" />
<colgroup align="left" />
<colgroup align="left" />
<tr><th>Constant</th><th>Value</th><th>Description</th></tr>
<tr><td><code>top</code></td><td>0</td><td> Panel placed at top of the screen. </td></tr>
<tr><td><code>bottom</code></td><td>1</td><td> Panel placed at bottom of the screen. </td></tr>
<tr><td><code>left</code></td><td>2</td><td> Panel placed at left of the screen. </td></tr>
<tr><td><code>right</code></td><td>3</td><td> Panel placed at right of the screen. </td></tr>
</table>
         */
        public static final int position=0x7f010001;


大概是上面这样子,我需要匹配出 openedHandle出来即可。

当然实际上我可以先判断每行有没有public static final int这个字符串,有的话就trim一下,在把=号以及后面的去掉,之后再截一下字符串就可以了。但太复杂了。。。有需求才有进步,借此想认真的学一下正则表达式,之前自己学的那些只能匹配简单的。一复杂就不行了

------解决方案--------------------
你这个问题太简单,自己去花30分钟看看,30分钟快速入门到精通正则:
http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html
------解决方案--------------------
for example
Java code
Scanner sc = new Scanner(new FileInputStream("your_file"));
String regex = "(?i).*?public static final int (.*?)\\s*=.*";
Pattern p = Pattern.compile(regex);
while (sc.hasNext()) {
    String buf = sc.nextLine();
    if (! buf.matches(regex)) continue;
    Matcher m = p.matcher(buf);
    while (m.find()) System.out.println(m.group(1));
}