Home > OS >  Regex: remove www. from domain name only for domain-strings whit two or more dots
Regex: remove www. from domain name only for domain-strings whit two or more dots

Time:12-20

Have a real domain names like www.by or www.ru, so for such domains I wanna keep "www.". But for other domains, like www.example.org or www.sub.example.org "www." need to be removed.

I have tried regex such ^(www[0-9]*\.)(\.*){2,} but is not working... Have any ideas?

CodePudding user response:

You can replace matches of the following regular expression with empty strings:

^www\d*\.(?=.*\.)

The positive lookahead (?=.*\.) asserts that the first period is followed later in the string by another period.

Demo

CodePudding user response:

You can use

^www[0-9]*\.((?:[^.]*\.) )

Replace with $1. See the regex demo. Details:

  • ^ - start of string
  • www[0-9]*\. - www, any zero or more digits, and a .
  • ((?:[^.]*\.) ) - Group 1 ($1 refers to this value from the replacement pattern): one or more occurrences of zero or more chars other than a . and then a . char.
  • Related