Whole word match when wildcard exist in search pattern RRS feed

  • Question

  • Hi all,

    I encounter a case when using Regex.

    My search pattern is like "He*lo", and I want to find whole word matched only.

    So my search pattern will be searchpattern="(\\W|^)" + "He*lo"+ "(\\W|$)"

    Search text: "HellPokkllo"   --- It returns correct result using Regex.IsMatch("HellPokkllo",searchpattern)

    However, if my search text likes "Hel  Pokkllo"---expected result will be not match because there exists space in text, but actually it returns true. It considers search text with space as one word.

    How to handle it?

    Hard hard work, Day day up!

    Thursday, December 20, 2012 9:01 AM


All replies

  • Try this

    searchpattern="(\\W|^)" + "He[a-zA-Z]+lo"+ "(\\W|$)"


    Thursday, December 20, 2012 1:30 PM
  • Thank you for your reply.

    Actually, I know that it works if I use [a-zA-Z], but I have to stay with pattern "He*lo", at least, I have to keep "He*lo", add pattern is allowed, alter is not...

    Is there any solution else?

    Thanks again.

    Hard hard work, Day day up!

    Thursday, December 20, 2012 2:03 PM
  • YOu have to change the "He*lo" because the * includes a white space and you need to exclude the white space.  Is [^ ] acceptable?


    Thursday, December 20, 2012 2:26 PM
  • Hi Joel,

    If I have to change the pattern, what's the best pattern?

    My concern is that I used "\W"(match whole word only, right?) in original pattern, why it matchs "\W" even search text has whitespace there?

    Hard hard work, Day day up!

    Friday, December 21, 2012 1:51 AM
  • Hi Stephan,

    Thank you for posting on this forum.

    When you need to change the pattern, please try the suggestion Joel provided earlier.

    "\W" means any non-word characters: http://msdn.microsoft.com/en-us/library/az24scfc.aspx 


    Matches any word character.


    "I", "D", "A", "1", "3" in "ID A1.3"


    Matches any non-word character.


    " ", "." in "ID A1.3"

    I hope this is clear.

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 21, 2012 6:26 AM
  • The pattern \S+ is the closest to your orignal pattern which will match any characters except white spaces.  So try this

    searchpattern="(\\W|^)" + "He\S+lo"+ "(\\W|$)"


    Friday, December 21, 2012 8:59 AM