Home > Software design >  Removing last character before ' : ' using regular expressions
Removing last character before ' : ' using regular expressions

Time:08-14

I just started learning regex and couldn't find a way to make this happen, even looked everywhere in the forum and haven't found a clue.

If someone can help and explain me the reasoning behind it, I'll appreciate it a lot, thank you.

I have the following:

martin123:123martin
scoprio8881:447juean

I want the following:

martin12:123martin
scoprio888:447juean

Thank you!

CodePudding user response:

Search for (.*?).:(.*) and replace it with $1:$2

Here is it in regexr: https://regexr.com/6rr1g

In short - it looks for

  • Anything, but non-greedy (so that it doesn't consume the next part). Put this in the first group by putting it in parentheses
  • A single character (the one that you don't want)
  • A colon
  • The rest, also in a group

Now replace it with group 1, a colon, and finally group 2

CodePudding user response:

Search for .(?=:) and replace with an empty string.

  • . the character that has to be removed
  • (?=:) positive look-ahead for colon

https://regex101.com/r/0FkXeH/1

  • Related