Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use import java.util.regex.Pattern ?

 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Result for complied above code

java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*welcome*
^
at java.util.regex.Pattern.error(Pattern.java:1528)
at java.util.regex.Pattern.sequence(Pattern.java:1645)
at java.util.regex.Pattern.expr(Pattern.java:1545)
at java.util.regex.Pattern.compile(Pattern.java:1279)
at java.util.regex.Pattern.<init>(Pattern.java:1035)
at java.util.regex.Pattern.compile(Pattern.java:779)
at com.spns.history.TestPattern.main(TestPattern.java:21)
Exception in thread "main"


Why i don't use "*" in pattern?

--------
p.s. When i remove "*" is work fine.
 
Amit Ghai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The '*' character by itself does not mean anything in regular expressions. If it follows a character, character class, or group, then it means 'zero or more' of the thing it follows.

If your intention with "*welcome" is "zero or more of any character, welcome" then the expression you want is: ".*welcome";
The '.' character will match any character, and the '*' works as stated above.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic