Reputation: 493
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!
Upvotes: 0
Views: 1072
Reputation: 493
Thanks to aliteral mind, I learned about non capture groups and that was key...
Behold!...
Host(?:\s=\s|=\")(.*?)(?:\||\")
Upvotes: 0
Reputation: 46841
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[\\\"|\\|]");
Upvotes: 0
Reputation: 20899
For the example given, you could use:
^Host\s*=\s*(?:")?(?:[^|"])+[|"]$
it will also accept
host=server.domain.com"
but if the logs are either / or that shouldn't be an issue.
Upvotes: 1