Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing last character before ' : ' using regular expressions

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

martin123:123martin
scoprio8881:447juean

I want the following:

martin12:123martin
scoprio888:447juean

Thank you!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading