0

I'm attempting to match anything between different delimiters, using a Java based regex engine/interpreter. The text I'm after is the server.domain.com I do not believe I can use any Java either, only a regular expression. The log will only have one OR the other, never both. This must be accomplished with a single regex, or the application will have to be re-written.

Examples of the logs:

Host = server.domain.com|

OR

Host="server.domain.com"

Thus far I've tried the following, along with a number of other combinations...

Host="(.*?)"|Host\s=\s(.*?)\|

I must also use Host as part of the delimiter, as it is parsing out of a log with many other similar pieces.

Thanks for any help on this!

MrMr
  • 411
  • 1
  • 9
  • 18
  • And can there be any embedded double quotes/pipe signs? – fge Apr 08 '14 at 21:32
  • I'm attempting to extract the server.domain.com text btw. =) – MrMr Apr 11 '14 at 15:29
  • 1
    There are two questions in the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) that may help, which are both listed under "Common Validation Tasks > Internet": [matching urls](http://stackoverflow.com/a/190405/2736496) and the [host/port part](http://stackoverflow.com/a/22697740/578411) – aliteralmind Apr 11 '14 at 15:43
  • Dear Mr. aliteralmind, I <3 you very much! I just learned about using non capture groups and that did the trick! See my answer below... – MrMr Apr 11 '14 at 16:04

3 Answers3

1

For the example given, you could use:

^Host\s*=\s*(?:")?(?:[^|"])+[|"]$

Regular expression visualization

Debuggex Demo

it will also accept

host=server.domain.com"

but if the logs are either / or that shouldn't be an issue.

dognose
  • 18,985
  • 9
  • 54
  • 99
  • This is the right approach. Regular expressions aren't the tool you want to validate that your delimeters match. – Patrick Collins Apr 08 '14 at 22:25
  • This is a great response and explanation, but it does not work. I've even tried removing the anchors and nothing... =/ It highlights the entire selection and not just the server.domain.com. – MrMr Apr 11 '14 at 15:28
0

Try this one with both the string

    String str1 = "Host = server.domain.com|";
    String str2 = "Host=\"server.domain.com\"";

    //Host(no or one space)=(" or one space)server.domain.com(| or ")
    Pattern p = Pattern.compile("Host\\s?=[\\\"|\\s]server.domain.com[\\||\\\"]");
    Matcher m = p.matcher(str1);
    if (m.find()) {
        System.out.println("found");
    }

You can try this one also if no of spaces are not known on either side of equal to.

//Host(zero or more spaces)=(zero or more spaces)(" or spaces)server.domain.com(" or |)
Pattern p = Pattern.compile("Host\\s*=\\s*[\\\"|\\s*]server.domain.com[\\\"|\\|]");
Braj
  • 44,339
  • 5
  • 51
  • 69
  • I tried both of the regex's, but it did not match anything. Also, I'm not using any code, only the regular expression. – MrMr Apr 11 '14 at 15:34
  • Damn it you guys rock! Thanks Braj – MrMr Apr 11 '14 at 16:24
  • Now you can test your Regex here and Please tell me what pattern is not matched by given Regex in my post. – Braj Apr 11 '14 at 16:48
0

Thanks to aliteral mind, I learned about non capture groups and that was key...

Behold!...

Host(?:\s=\s|=\")(.*?)(?:\||\")

MrMr
  • 411
  • 1
  • 9
  • 18