-1

I have the below test case,

    @Test
    public void test_check_pattern_match_caseInSensitive_for_pre_sampling_filename() {
        // given
        String pattern = "Sample*.*Selection*.*Preliminary";

        // when

        // then
        assertThat(Util.checkPatternMatchCaseInSensitive(pattern, "Sample selectiossn preliminary"), is(false));
        assertThat(Util.checkPatternMatchCaseInSensitive(pattern, "sample selection preliminary"), is(true));
    }

The Util method is:

public static boolean checkPatternMatchCaseInSensitive(String pattern, String value) {

        Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(value);
        if (matcher.find())
            return true;

        return false;
    }

Can someone please help, why the regex Sample*.*Selection*.*Preliminary matches the fileName = Sample selectiossn preliminary ?

This test case should pass, but it fails because of the first assert. :S

3 Answers3

2

The * in regex means 0 or more of the previous character, while . means any single character.

What your expression is looking for is:

  1. Exactly Sampl
  2. 0 or more e
  3. 0 or more of any char
  4. Exactly Selectio
  5. 0 or more n
  6. 0 or more of any char And so on

The problem would fall under points 5 and 6:

No n was found under point 5, and ssn would match point 6

Justin Tay
  • 74
  • 4
0
  • Selection* in regexp matches to "selectio".
  • .* matches to "ssn "
  • Preliminary matches to "preliminary"

Regexp n* mean zero or more n character.

Regexp . mean any character.

Regexp .* mean zero or more any character.

talex
  • 15,633
  • 2
  • 24
  • 56
0

*.*

You have "Selection*.*", which means "Selectio", then any number (including zero) of letter "n", then any number (including zero) of any character.

The match assumes zero matches of "n" matching "", and four matches of any character matching "ssn ".

Amadan
  • 169,219
  • 18
  • 195
  • 256