1

I need to fetch hostname from a fully qualified domain name String. All parental domain / subdomain / interface names should be stripped, i.e.

abc001 -> abc001     ## stays name ##
efg.fqdn.com -> efg  ## only 1st/short name ##
mnop-int -> mnop     ## only 1st/short name ##
help-adm.fqdn -> help  ## only 1st/short name ##

I've tried various combinations similar to this:

(.*?)(?:.|-)

but it didn't help much.

PS: The regex should be a single-liner, as it is goes as an input in application.

3
  • 1
    ... and the Question?? Commented Oct 19, 2015 at 11:48
  • don't you think help-adm and mnop-int should be part of answer. i.e. mnop-int.subdomain.domin Commented Oct 19, 2015 at 12:59
  • @Manoj, not exactly...if the server is resolvable by a different interface, such as server-int or server-adm we have to remove the '-int' or '-adm' from it's name. Commented Oct 19, 2015 at 13:31

3 Answers 3

1

If I understand your requirement right, you want to get the part of a String before the first dot or hyphen.

In this case, the following regex will work: "([^.-]*)[.-]?". The first match will be the desired String.

Test code:

public static void main(String[] args) throws Exception {
    String[] strs = { "abc001", "efg.fqdn.com", "mnop-int", "help-adm.fqdn" };
    Pattern pattern = Pattern.compile("([^.-]*)[.-]?");
    for (String str : strs) {
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }
}

Output:

abc001
efg
mnop
help
Sign up to request clarification or add additional context in comments.

2 Comments

yes your understanding is correct, but when I run the regex, it's showing all separated values as output, instead of just the first separated value..
@Marcos I just re-ran the sample code in my answer and this works fine.
1

Regex flavor other than Java-

^.*?\\b

You can \b or word boundary here.See demo.

https://regex101.com/r/tD0dU9/3

word boundary \b won't work in this case as Java includes all unicode characters range as a word boundary condition.

Comments

0

As Java's \b boundary condition is little bit different than other regular expression engine, We need to take more care when we are using boundary conditions.

public class BoundaryCondRegEx {

 public static void main(String[] args) {

    Pattern p = Pattern.compile("^(?<n>\\w+).*$");
    Matcher m = p.matcher("mnop-int");
    if(m.matches())
    {
        System.out.print(m.group("n"));
    }
    else
        System.out.println("Nope");

   }

}

So As mentioned in pattern "^(?<n>\\w+).*$"
\w is representing [0-9A-Za-z_]
if your resultant substring is made of these chars, you are fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.