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?
Asked
Active
Viewed 554 times
0
-
1Please 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 Answers
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.
-
1Java 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
-