Issue
I tried this regex in Java :
Pattern orderCheck = Pattern.compile("^([\\-\\+])?(created|title|price|creation)$", Pattern.CASE_INSENSITIVE);
orderCheck.matcher(value).matches();
And oddly :
- created : works
- -created : works
- +created : doesn't works
- title : works
- -title : works
- +title : doesn't works
- etc..
The + result as a non validation of the regex, but I can't understand why.
Using this online regex tester, it works perfectly, for all the values !
I also tried this variants, but unsuccessfully :
- ^[\-\+]?(created|title|price|creation)$
- ^[-+]?(created|title|price|creation)$
- ^(-+)?(created|title|price|creation)$
- ^(-|+)?(created|title|price|creation)$
- ^[-|+]?(created|title|price|creation)$
- ^(\-|\+)?(created|title|price|creation)$
What is the correct regex for my need? I can't see where I'm wrong.
Solution
It works. Maybe you forgot a space char at the end/begining of your value? Your pattern seems OK.
Answered By - Erhan Bagdemir
Answer Checked By - Cary Denson (JavaFixing Admin)