求一个正则表达式。另外求正则表达式学习资料

追风筝的孩子 2012-05-03 10:41:16
      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一下,在把=号以及后面的去掉,之后再截一下字符串就可以了。但太复杂了。。。有需求才有进步,借此想认真的学一下正则表达式,之前自己学的那些只能匹配简单的。一复杂就不行了
...全文
156 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
追风筝的孩子 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 a328240784 的回复:]
引用 3 楼 的回复:
没看明白你要做什么!

如果只看 public static final int 的话,那为什么只能找出 openedHandle 呢?我认为最下面的那个 position 也能匹配!

PS:如果 public static final int openedHandle=0x7f010007; 位于注释里面也需要匹配么?
我那个是android工程,R文件,……
[/Quote]应该说不会把public static int xxx注释。。我还是好好学下正则吧 = =
追风筝的孩子 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
没看明白你要做什么!

如果只看 public static final int 的话,那为什么只能找出 openedHandle 呢?我认为最下面的那个 position 也能匹配!

PS:如果 public static final int openedHandle=0x7f010007; 位于注释里面也需要匹配么?
[/Quote]我那个是android工程,R文件,所以不会有注释
  • 打赏
  • 举报
回复
没看明白你要做什么!

如果只看 public static final int 的话,那为什么只能找出 openedHandle 呢?我认为最下面的那个 position 也能匹配!

PS:如果 public static final int openedHandle=0x7f010007; 位于注释里面也需要匹配么?
  • 打赏
  • 举报
回复
你这个问题太简单,自己去花30分钟看看,30分钟快速入门到精通正则:
http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html
追风筝的孩子 2012-05-03
  • 打赏
  • 举报
回复
应该说我需要匹配出所有public static final int xxxxxxxxx=yyyyyyyyyy 的xxxxxxxxxxx出来
追风筝的孩子 2012-05-03
  • 打赏
  • 举报
回复
为什么大家都木有看我1楼呢。。。。

我算写出来了。。。2楼的教程不错。
public static final int (.+)\\s*[=]

第二个组里。。虽然我不明白为什么是第二个组而不是第一个组= =

安特矮油 2012-05-03
  • 打赏
  • 举报
回复
用正则到是可以去出所有类似public static final int 的属性的名称。例如你上面的代码,那么用比较通用点的正则那么会取出openedHandle和position如果需要固定匹配openedHandle,那么只需要.+?\\s+openedHandle.+?这样的正则就可以筛选出openedHandle的行了
qybao 2012-05-03
  • 打赏
  • 举报
回复
for example
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));
}


正则的学习资料,LZ可以参考javadoc的Pattern类,在java.util.regex包里

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧