1

I need to match two words that usually are joined with a dash '-' and sometimes 0, 1 or 2 white spaces. I decided to use the following Regex pattern to solve the problem: [.\s]*

However that pattern does not match this case: "word1-word2" The following pattern solves the problem: .*\s* for all the cases.

All the cases:

word1-word2
word1word2
word1 word2
word1 word2

I am currently using the second pattern, however I would like to know the subtle diference between them. If it is of any help I am using C#.

Regards

Ayorus
  • 467
  • 3
  • 14

3 Answers3

2

When a . is in [], it matches the literal character ..

When it is used outside a [], it matches any character except for a new line.

You may see the explanation here:

Though, you may consider using \b\w*\s*-\s*\w*\b for your case. See the example at https://regex101.com/r/cJ4kB9/3

Harsh Poddar
  • 2,136
  • 15
  • 17
0

You regex is basically saying:

.  // Match any character
*  // Zero or more times
\s // Matches any white space including space, tab, form-feed, etc.
*  // Those white spaces, tabs etc. can occur Zero or more times.

So by the looks of it, it basically fails to identify if there is a 2nd word next to it. It would also validate against:

word1 -

word1-

etc.

Vivendi
  • 16,485
  • 22
  • 93
  • 175
0

[^\s]+[\s-]*[^\s]*

A couple of non-whitespace characters, followed by 0 or more whitespace/dash combos, followed by a couple of non-whitespaces again.

JimmyB
  • 11,178
  • 1
  • 23
  • 42