Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Regex to Match "abba", "xyyx" but NOT match "aaaa","xxxx"

gawa
16 - Nebula
16 - Nebula

I came across an issue how to write Regex in the below case. 

I want to match 4 alphabet letters. 1st letter can be any letter, 2nd letter is different letter from 1st letter, 3rd letter is the same as 2nd letter, and 4th letter is the same as 1st letter:  
 abba

 deed
 xyyx

 

However, it should not match 4 identical alphabet letters like:
 aaaa

 eeee
 zzzz

 

I thought the following Regex would work, but Alteryx Designer throws an error. It seems some syntax error exist around [^\1] that I intended to specify "Not a letter captured at first position".

REGEX_Match([String], "^(\w)([^\1])\2\1$")

image.png

 

As a workaround, I had to modify the expression like this, and it worked.

REGEX_Match([String], "^(\w)(\w)\2\1$") and !REGEX_Match([String], "^(\w)\1{3}$")

Though I found workaround, I'm still wondering why my first Regex expression (\w)([^\1])\2\1 had a syntax error, and how should I re-write it keeping a single Regex expression without AND/OR operator? If you could advise on this, it would be appreciated.

4 REPLIES 4
flying008
15 - Aurora

Hi, @gawa 

 

FYI.

 

 

 

REGEX_Match([Txt], '^([a-z])(?!\1)([a-z])\2\1$')

 

 

 

录制_2024_01_27_16_30_20_47.gif

binuacs
21 - Polaris

@gawa similar to @flying008 solution

image.png

Swathi
9 - Comet

In Alteryx, backslashes are used as escape characters in regular expressions, and you might need to double-escape them. Try using four backslashes before digit reference \1

gawa
16 - Nebula
16 - Nebula

@flying008 @binuacs 

Thank you very much, I didn't know "Negative Lookahead" so it's a really good learning for me. 

Positive Lookahead (?=pattern)

Negative Lookahead (?!pattern)

 

There remains a lot to learn on Regex for me. Thank you again!

Labels