Nate
Nate

Reputation: 7876

Remove space of string and compare using RegEx

I have the following string:

Name was changed from *** to %%%

while *** and %%% can be anything.

What I need to do is to make sure that *** and %%% are different. I also need to make sure that if *** equal Yan n and %%% is Yann they will be considered as similar (white spaces can occur before and/after the variable).

My current RegEx only detect the sentence itself:

^Name was changed from.*to.*$

How can I make sure that it doesn't take into account when *** and %%% are identical (excluding the whitespaces)?

FYI I use this regex in VBA/Excel macro

Upvotes: 0

Views: 111

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

It seems based on the comments using a capturing group in combination with a negative lookahead and a backreference to the group would be sufficient to get the matches:

^Name was changed from (.+?) to (?!\s*\1\s*$).*$

Regex101 demo

Explanation

  • ^ Start of string
  • Name was changed from (.+?) to Match starting text and capture in a group non greedy what is between from and to
  • (?! Negative lookahead, assert what is directly on the right is not
    • \s*\1\s*$ Match 0+ whitespace chars, backreference to group 1, 0+ whitespace chars and assert end of the string
  • ) Close lookahead
  • .*$ Match any char until the end of the string

Upvotes: 1

Related Questions