英文:
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.
(.*?\.)?(.*\..*)
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论