English 中文(简体)
我如何定期表达与野心相当的一部分?
原标题:How do I get the matched part of a wildcard in a regular expression?
  • 时间:2011-10-30 03:36:09
  •  标签:
  • java
  • regex

例如:

Pattern pattern = Pattern.compile("a(.*)b");
Matcher matcher = pattern.matcher("a19203b");
matcher.find();
System.out.println(matcher.group());

这部印刷版(a19203b)。 所有我都需要<代码>19203。 我如何在 Java这样做?

(例如,在改写规则中,我想做的是Rewrite Rule article/(*)条。) 第1条: <代码>1 是第1条所示

最佳回答

找到解决办法。 不用matcher.group(),使用matcher.group(1)

Pattern pattern = Pattern.compile("a(.*)b");
Matcher matcher = pattern.matcher("a19203b");
matcher.find();
System.out.println(matcher.group(1));
问题回答

1. 使用表面/head头:

Pattern regex = Pattern.compile("(?<=a).*(?=b)");

不要抓住你不想要的东西。 在此,你所希望的就是你们。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签