0

I have a coupon code which dynamically generates with provided prefix, suffix and length

I have generated one coupon code with following param :

prefix  - NOV
Suffix  - OFF
Length  - 10

And coupon NOVQ3QVOFF generated. I have to validate where it generated with given prefix and suffix. for that I'm using below code:

String couponCode = "NOVQ3QVOFF";
String pattern = "^NOV*";
System.out.println(Pattern.compile(pattern).matcher(couponCode).matches());

But getting output as false

Also tried pattern = "^NOV"; but no success. Also i have validated regex online its matching there.

enter image description here

Can someone please help. whats wrong here ?

NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • 1
    `matches` requires a full string match. Use `find` to get partial matches. Note: `^NOV*` matches `NO` at the start of string and then 0 or more `V`. You probably wanted `^NOV.*` or even `^NOV.*OFF$`, in `matches`, you may omit the anchors and use `s.matches("NOV.*OFF")` or replace `*` with the limiting quantifier that will match a specified number of occurrences of any char, say, `.{6}` will match any 6 chars other than line break chars. – Wiktor Stribiżew Nov 14 '18 at 09:44
  • You most likely need `NOV.*` or even `NOV.{4}OFF` – Sebastian Proske Nov 14 '18 at 09:44
  • Try this [Regex demo](https://regex101.com/r/xSNych/1) – Michał Turczyn Nov 14 '18 at 09:50
  • @SebastianProske, Thanks you suggestion is helpful – NarendraR Nov 14 '18 at 09:53

0 Answers0