0

Here's my code:

String myRegex = "*cow"
String name = "SHIRACOWPEPPER"
name = name.toLowerCase()

if(!name || name.matches(myRegex)) {
    return true
}

When I run this I get a PatternSyntaxException: Dangling meta character '*' near index 0 *cow ^ error. Ideas?

DirtyMikeAndTheBoys
  • 1,077
  • 3
  • 15
  • 29

2 Answers2

2

The * is a meta character which means 'zero or more times' the thing you matched before, but in this case there is nothing to match. This should probably work:

String myRegex = ".*cow"
String name = "SHIRACOWPEPPER"
name = name.toLowerCase()

if(!name || name.matches(myRegex)) {
    return true
}

For more information see the documentation

ebo
  • 2,717
  • 1
  • 27
  • 22
0

You probably meant to say String myRegex = ".*cow" which means any number characters prior to cow that is the suffix.

dimitrisli
  • 20,895
  • 12
  • 59
  • 63