Take the 2-minute tour ×

I have a program where the user will enter a string such as PropertyA = "abc_*" and I need to have the asterisk match any character.

In my code, I'm grabbing the property value and replacing PropertyA with the actual value. For instance, it could be abc_123. I also pull out the equality symbol into a variable.

It should be able to cover this type of criteria

PropertyB = 'cba'

PropertyC != '*-this'

valueFromHeader is the lefthand side and value is the righthand side.

if (equality.equals("=")) {
    result = valueFromHeader.matches(value);
} else if (equality.equals("!=")) {
    result = !valueFromHeader.matches(value);


The existing code had this type of replacement for regular expressions

    final String ESC = "\\$1";
    final String NON_ALPHA = "([^A-Za-z0-9@])";
    final String WILD = "*";
    final String WILD_RE_TEMP = "@";
    final String WILD_RE = ".*";
    value = value.replace(WILD, WILD_RE_TEMP);
    value = value.replaceAll(NON_ALPHA,ESC);
    value = value.replace(WILD_RE_TEMP, WILD_RE);

It doesn't like the underscore here...

abcSite_123 != abcSite_123 (evaluates to true)

It doesn't like the underscore...

share|improve this question
What isn't working with what you have tried so far? –  Scott Hunter Oct 18 '13 at 14:33
Please include your entire method, and state what isn't working. –  TJamesBoone Oct 18 '13 at 14:36
I replaced the original problem with some existing code that was already present. –  envinyater Oct 18 '13 at 14:56

1 Answer 1

up vote 2 down vote accepted

Replace * with .*..

value=value.replace("*",".*");//replace  * with .*

This won't work if your value contain +,? since they all have special meaning in regex.Escape them if their are any..

share|improve this answer
When testing this, if I give it abcSite != 'abc*' I get abcSite != \Qabc*\E and it evaluates to True (based on the if statement which negates the matching). –  envinyater Oct 18 '13 at 15:05
Just to logically make it easier to make sure my logic is alright, I tried abcSite = 'abc*' and it evaluates as false –  envinyater Oct 18 '13 at 15:22
@envinyater hope the edit helps –  Anirudha Oct 18 '13 at 15:44
That worked! Let me test it against some more input. –  envinyater Oct 18 '13 at 15:47
Seems to be working against all of my input. Thank you! –  envinyater Oct 18 '13 at 16:59

Your Answer


