正则表达式:替换第一次出现之前的所有内容

huangapple go评论120阅读模式
英文:

Regular expression: Replace everything before first occurence

问题

我有以下正则表达式,用于移除我的URL中的dev.部分。

String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll(".*\\.(?=.*\\.)", ""));

输出:mydomain.com 但是当域名为dev.mydomain.com.pe或者dev.mydomain.com.uk时,我得到的结果只有.com.pe.com.uk部分。

是否有一个修饰符可以在我的正则表达式上使用,以确保它只获取第一个.之前的部分(包括点)?

期望输出:

dev.mydomain.com -> mydomain.com

stage.mydomain.com.pe -> mydomain.com.pe

test.mydomain.com.uk -> mydomain.com.uk

英文:

I have the following regular expression that I'm using to remove the dev. part of my URL.

String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll(".*\\.(?=.*\\.)", ""));

Outputs: mydomain.com but this is giving me issues when the domains are in the vein of dev.mydomain.com.pe or dev.mydomain.com.uk in those cases I am getting only the .com.pe and .com.uk parts.

Is there a modifier I can use on my regex to make sure it only takes what is before the first . (dot included)?

Desired output:

dev.mydomain.com -> mydomain.com

stage.mydomain.com.pe -> mydomain.com.pe

test.mydomain.com.uk -> mydomain.com.uk

答案1

得分: 1

你可以使用以下正则表达式:

^[^.]+\.(?=.*\.)

查看正则表达式示例正则表达式图示

正则表达式:替换第一次出现之前的所有内容

细节

  • ^ - 字符串开头
  • [^.]+ - 1个或多个非点字符
  • \. - 一个点
  • (?=.*\.) - 跟随任意0个或多个非换行字符,尽可能多,然后是一个点。

Java使用示例:

String result = domain.replaceFirst("^[^.]+\\.(?=.*\\.)", "");
英文:

You may use

^[^.]+\.(?=.*\.)

See the regex demo and the regex graph:

正则表达式:替换第一次出现之前的所有内容

Details

  • ^ - start of string
  • [^.]+ - 1 or more chars other than dots
  • \. - a dot
  • (?=.*\.) - followed with any 0 or more chars other than line break chars as many as possible and then a ..

Java usage example:

String result = domain.replaceFirst("^[^.]+\\.(?=.*\\.)", "");

答案2

得分: 0

以下正则表达式适用于您的情况。它将查找第一部分(如果存在),捕获字符串的剩余部分作为第二个匹配组,并用第二个匹配组替换字符串。.*?是非贪婪搜索,它将匹配直到遇到第一个点字符为止。

(.*?\.)?(.*\..*)

正则表达式演示

示例代码:

String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "stage.mydomain.com.pe";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "test.mydomain.com.uk";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

输出:

mydomain.com
mydomain.com.pe
mydomain.com.uk
mydomain.com
英文:

Following regex will work for you. It will find first part (if exists), captures rest of the string as 2nd matching group and replaces the string with 2nd matching group. .*? is non-greedy search that will match until it sees first dot character.

(.*?\.)?(.*\..*)

Regex Demo

sample code:

String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "stage.mydomain.com.pe";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "test.mydomain.com.uk";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

domain = "mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));

output:

mydomain.com
mydomain.com.pe
mydomain.com.uk
mydomain.com

huangapple
  • 本文由 发表于 2020年7月29日 00:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定