English 中文(简体)
在java的reg中,寻找特定扼杀模式?
原标题:regex in java, making a search for particular string pattern?
  • 时间:2012-01-12 10:37:12
  •  标签:
  • java
  • regex

i am not much familiar with regex expressions use in Java. If i have a String this/is/a/file!/path

now with substring and indexOf i can find the name file appearing inbetween / and ! but i am pretty sure such tasks must be much easier using regexes.

难道有人能给我一个榜样,用雷克语这样做?

最佳回答

另一种办法是使用模式和匹配器。 在这方面,你可以使用团体和更为复杂的行动

Pattern pattern = Pattern.compile(yourRegex);
Matcher matcher = pattern.matcher(yourText);
while(matcher.find()) {
   System.out.println(matcher.group(1));
}

检查

Beller :

模式:

Regex in Java : http://docs.oracle.com/javase/tutorial/essential/regex/

问题回答

∗. 然后,你可以使用配对器找到第1组(母体之间的部分,0组为整个配对)

I didn t test this solution...

You can create a Pattern Object with a regular expression and then create a matcher object from it to parse your String.

String regex = ".*/(.*)!";

Pattern p = Pattern.compile (regex);

Matcher m = p.matcher(StringToBeMatched);

if (m.find()) {

    String filename = m.group (1);
}

Java最简单的使用方式是使用<代码>。 String.matches 方法:

boolean matches = "abc".matches("[a-z]+");

这将产生<代码>true。

这样做的第二种方式是,如果你不得不使用一种特殊的规管模式,则是有益的。 您可以汇编并重新使用:

Pattern pattern = Pattern.compile("[a-z]+");
Matcher matcher = pattern.matcher("abc");
boolean matches = matcher.matches();




相关问题
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 ...

热门标签