-1

I know regex isn't the best option for excluding but not sure about other options

I want to replace mobile version of websites, so I want to find and replace the "m." version of websites

https://m.gsmarena.com/sony_xperia_5_iv-price-11838.php

should be

https://gsmarena.com/sony_xperia_5_iv-price-11838.php

However, when I try and find an replace "m." this includes websites like this

https://redfm.ie/

becomes

https://redfie/

which changes the website name.

I am using spyder, and pandas to write this script.

I've tried following the steps in this page (Regular expression to match a line that doesn't contain a word) and the best I have at the moment is

m\.^((?!(\.ie|.com)).)*$

Or to not match when the "m." is followed by .ie or .com, but I'm not having much luck with it

Thanks!

Aki
  • 137
  • 1
  • 4
  • 17

1 Answers1

1

You can use \b to match word boundary.
\bm\. can match mobile version of websites.

Hence you would end up with the following regex to find and replace all your URLs no matter whether they contain a "m" or not : /(.*)(?:\bm\.)(.*)|(.*)/$1$2$3/g, see the Demo on regex101.

chooz
  • 41
  • 2