Forum Discussion

Timble's avatar
Timble
Copper Contributor
Mar 13, 2025
Solved

Multiple spaces in a regex

Hello folks.

I was trying to write a regex that would detect

Secret
S E C R E T
S  e  c  r  e  t
se C re T

ie, 'secret', case-insensitive and possibly with spaces between the letters (manual kerning)

When I use 

(?i)\bs[ ]?e[ ]?c[ ]?r[ ]?e[ ]?t\b

Then the first two cases succeed and words like "secretive" or "nonsecret" are not detected (which is correct). I'm aware that I could probably also just do " ?" (space, question mark) as there is a single character in the set.

I tried swapping the "?" question mark (zero or one occurrences) to an "*" asterisk (zero or many occurrences), "*?" asterisk, question mark (lazy zero or many occurrences) and then " {0,}" all of which work fine in regex101 but, at least in Exchange Online, they fail test three and four.

Has anyone got any advice on what I might try for multiple spaces? Is Exchange swapping two spaces for a completely different character?

Ta,

Tim

 

  • Thanks for reaching out. Try this pattern (?i)\bs\s*e\s*c\s*r\s*e\s*t\b to see if it fits your scenario.

2 Replies

  • Thanks for reaching out. Try this pattern (?i)\bs\s*e\s*c\s*r\s*e\s*t\b to see if it fits your scenario.

    • Timble's avatar
      Timble
      Copper Contributor

      Thank you, Milgo. That works in a SIT. I hadn't tried my original regexes there, and haven't tried your solution in a rule, but I'm happy that there is a solution. I'll probably continue testing, and certainly think about the risks of having all whitespace and not just spaces in the acceptable optional separators list.

Resources