0

I have these two strings for example: "this_is_a string" and this string "this_is_astring". I want to detect both instance using the %ilike%.

I know how to detect the space:

%ilike% "this_is_a\\sstring"

And it's obvious how to detect without space:

%ilike% "this_is_astring"

But how to mix the two in an expression that detects both at the same time. Sth like:

%ilike% 'this_is_a[\\s-'']string'
s_baldur
  • 29,441
  • 4
  • 36
  • 69
GitZine
  • 445
  • 3
  • 14

1 Answers1

0

The solution is to add a wild card, which the %ilike% detects 0 or more matches to it. In this case, it's either nothing or a space character. The wild card in this case is "*" added after the character that we want to detect.

So the answer is this:

%ilike% 'this_is_a\\s*string'

Update: As per another suggestion, this can better be solved using "?" after a space, which indicates a "0 or 1-time" match to the space character in that location.

  %ilike% 'this_is_a\\s?string'
GitZine
  • 445
  • 3
  • 14
  • 1
    `*` means "0 or more times", which works here. An alternative (depending on your use-case) is `?` which means "0 or 1 times" (but does not consume more if present). I find https://stackoverflow.com/a/22944075/3358272 to be a good reference for many-things-regex. – r2evans Dec 12 '22 at 00:15
  • Thanks for the suggestion. Actually, here the ? is more fitting as my use case is just 0 or 1 space. Thanks – GitZine Dec 12 '22 at 00:17