I recently asked for and received help regarding a Java regular expression that would match a string containing a maximum of two asterisks, which could be the first or last character, or both. This was the answer I accepted:
^\\*?[^*]+\\*?$
Naturally, the requirements have now changed and I need to further constrain the string such that it can only contain either lower or upper case letters, spaces or the asterisk wildcard character. No numbers or punctuation (other than *) are allowed. The constraints on the wildcard positioning remain as described above.
So these strings would be matched:
foo f oo *Foo FOO * *foo* *F o o*
And these strings would not:
* ** **foo *f*oo 1 *1 _ foo1* _foo %foo^
I have spent ages trying to work this out and haven’t been able to, even using RegexBuddy. I don’t really like asking such a specific question on Stack Overflow, but I just can’t get to the solution on my own.
Thanks in advance.
This should work: