具有TLD或localhost的正则表达式域

时间:2017-10-01 03:47:51

标签: regex

基本上,用户可以将我的音乐应用程序映射到以下其中一个位置:

  • {subdomain}.{domain}.localhost
  • {domain}.localhost
  • localhost

基本上,即使用户在localhost访问,我也需要在正则表达式{domain}.localhost{subdomain}.{domain}.localhost中进行提取。有点失去了正则表达式。

所以:

mymusic.homemusic.localhost --> homemusic.localhost
homemusic.localhost --> homemusic.localhost
localhost --> localhost

编辑:我正在使用#!/ bin / sh

1 个答案:

答案 0 :(得分:0)

在一些正则表达式之后,我得到了一个简单的答案:

[\w-]+\.?(localhost)?$
  • [\w-]匹配任何字词,包括-
  • +多次。
  • \.匹配.
  • ?如果有。
  • (localhost)匹配该字词
  • ?如果有。
  • 字符串末尾的
  • $

有很多限制性选项(如果用户输入rubish垃圾),但这对我有用。