0

I want to implement pattern ' * ' text matching on java. What is the simplest way to do this? Is Pattern.java the best solution for this?

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
Martin
  • 767
  • 2
  • 9
  • 21
  • 1
    Please show us some examples demonstrating what it is *exactly* that you're looking to do. – NPE Oct 31 '11 at 11:07
  • matching what? strings? filenames? And what is the syntax? regex? glob? – Will Oct 31 '11 at 11:07

3 Answers3

4

Have a look into the regular expression package java.util.regex. You find a good starting point here.

Howard
  • 38,639
  • 9
  • 64
  • 83
1

If I understand correctly you want to match the text occurring between two single quotes in a string. The regex for this is '.*' and not '*'. Code for this will look like this

String input = "abcd'efg'hij";
Matcher matcher = Pattern.compile("'.*'").matcher(input); //initializes a matcher
System.out.println("Found ? " + matcher.find() + 
                   "\nFound what ? "+ matcher.group()); //prints 'efg'

In case you want to match '*' literally then use the regex '\\*' (escape * with a \)

Check out documentation on java.util.regex.Pattern and java.util.regex.Matcher classes.

Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
0

There isn't a built in glob-matcher for Java, but you can easily convert a glob to a regex and use a regex library.

Community
  • 1
  • 1
Will
  • 73,905
  • 40
  • 169
  • 246
  • 1
    Java 7 does have a builtin glob-matcher. See [FileSystem.getPathMatcher](http://download.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)) – Mark Rotteveel Oct 31 '11 at 11:26
  • neat; although it has special meanings with path boundaries and things – Will Oct 31 '11 at 11:43