Home > Blockchain >  Regex Swift: How to find Two words OR Find Two words with a dash
Regex Swift: How to find Two words OR Find Two words with a dash

Time:09-07

I have two regex patterns in Swift, both work for each case separately:

case twoWords = "(@\w \s\w )" = @User Name

case twoWordsWithDash = "@(\w \s\w \-\w )" = @User Name-Hyphen

Question:

How can I combine these two regex patterns in their respective strings, so the regex will configure EITHER twoWords or twoWordsWithDash??

What I want:

case twoWordsORtwoWordsWithDash = "(@\w \s\w )|@(\w \s\w \-\w )" = @User Name OR @User Name-Hyphen

But this fails, that OR operator | doesn't seem to work..

CodePudding user response:

You just need to switch the order so that the user name with dash has priority over the one without:

(@\w \s\w \-\w )|(@\w \s\w )

You can check it using regex101

  • Related