Zoltan
Zoltan

Reputation: 33

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

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?

Upvotes: 3

Views: 47

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110675

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

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

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.

Upvotes: 1

Related Questions