How can I provide an OR operator in regular expressions? - java

I want to match my string to one sequence or another, and it has to match at least one of them.
For and I learned it can be done with:
(?=one)(?=other)
Is there something like this for OR?
I am using Java, Matcher and Pattern classes.

Generally speaking about regexes, you definitely should begin your journey into Regex wonderland here: Regex tutorial
What you currently need is the | (pipe character)
To match the strings one OR other, use:
(one|other)
or if you don't want to store the matches, just simply
one|other
To be Java specific, this article is very good at explaining the subject
You will have to use your patterns this way:
//Pattern and Matcher
Pattern compiledPattern = Pattern.compile(myPatternString);
Matcher matcher = pattern.matcher(myStringToMatch);
boolean isNextMatch = matcher.find(); //find next match, it exists,
if(isNextMatch) {
String matchedString = myStrin.substring(matcher.start(),matcher.end());
}
Please note, there are much more possibilities regarding Matcher then what I displayed here...
//String functions
boolean didItMatch = myString.matches(myPatternString); //same as Pattern.matches();
String allReplacedString = myString.replaceAll(myPatternString, replacement)
String firstReplacedString = myString.replaceFirst(myPatternString, replacement)
String[] splitParts = myString.split(myPatternString, howManyPartsAtMost);
Also, I'd highly recommend using online regex checkers such as Regexplanet (Java) or refiddle (this doesn't have Java specific checker), they make your life a lot easier!

The "or" operator is spelled |, for example one|other.
All the operators are listed in the documentation.

You can separate with a pipe thus:
Pattern.compile("regexp1|regexp2");
See here for a couple of simple examples.

Use the | character for OR
Pattern pat = Pattern.compile("exp1|exp2");
Matcher mat = pat.matcher("Input_data");

The answers are already given, use the pipe '|' operator. In addition to that, it might be useful to test your regexp in a regexp tester without having to run your application, for example:
http://www.regexplanet.com/advanced/java/index.html

Related

Java - match specific URL in string

There must be something very simple that I'm missing here. I'm trying to match an exact URL in a given string. Here's the code :
String pattern = "\\b.*"+"\"http://fonts.googleapis.com/css?family=Montserrat:400,700\""+"\\b";
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher("<link href=\"http://fonts.googleapis.com/css?family=Montserrat:400,700\"");
System.out.println(m.find()); // returns false
But the same code works when I try for local resources :
pattern = "\\b.*"+"style.css"+"\\b";
p=Pattern.compile(pattern);
m=p.matcher("<link href=\"css/style.css\"");
System.out.println(m.find()); // returns true
You are missing the fact that in the URL you try and match, there is a question mark. And the question mark is a quantifier, which means it is treated specially by the regex engine (this quantifier means "zero or more of the previously recognized atom").
You do not want that question mark to be interpreted; which means your regex should be built differently... And there is a way:
final String quotedUrl
= Pattern.quote("http://fonts.googleapis.com/css?family=Montserrat:400,700");
final String regex = "\\b\"" + quotedUrl + "\"\\b";
final Pattern pattern = Pattern.compile(regex);
// work with the regex
Note that in fact, Pattern.quote() only ever surrounds your input with the regex special sequences \Q and \E. And those were borrowed from perl, unsurprisingly, since perl regexes have been the lingua franca of all successful regex engines so far.
Unless you have some other intention for this, the word boundary assertions and use of regex seem irrelevant here. I would suggest just using a non-regex solution using contains or indexOf.
String url = "http://fonts.googleapis.com/css?family=Montserrat:400,700";
String src = "<link href=\"http://fonts.googleapis.com/css?family=Montserrat:400,700\"";
System.out.println(src.contains(url));

Regex to Find Anything Between Multiple Delimiters

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

regex in java string

While trying some JAVA coding on the codingbat.com site, I came repeatedly to a Question about the functionality of regular expressions in java strings.
I know there are JAVA methods like matches() or finder() as well as replace() and so on, but this isn't where I wanted to go.
Take a quick look at the example:
boolean doubleX(String str) {
if(str.equals("xx")){
return true;
} else {
return false;
}
}
I wonder whether I could use regular expressions in the string to add a quantifier, for example
<----- add regex here
if(str.equals("x\[x.*]")){
Would you sirs, be so kind, to explain me, how I could use regex in strings? After all I understood, I thought, it would be possible even w/o using the java regex methodes, because the escape signal \ makes them usable even in plain code. Did I got this wrong?
Use String#matches(String)
if (str.matches(regex)) {
// ...
}
This will only find out if there is a match for the regex though.
What I suggest is that you specify the quantifier in your regex instead of counting the number of matches, like so:
public boolean isX(String str, int count) {
return str.matches("^x{" + count + "}$");
}
Some methods support regex as input and some is not. In general you can't use regex in plain String, because after all it will be just plain string. But some your or framework's methods can support regex inside with Pattern or other approaches.
You can use the Pattern and the Matcher class
private final Pattern PATTERN = Pattern.compile("x\[x.*]");
and then
Matcher matcher = PATTERN.matcher(str);
if (matcher.find())
doSomething();

getting matching regular expressions in java

String.split(String regex) splits the string around a given regular expression and returns an String array. But I am interested in the regex matches and would like them to be returned as string array instead of strings around them.
For example,
In case of trival regex like ":" it probably wouldn't matter. But there are regexes which would match a particular date in a paragraph and I would like to get all these dates which may be different each time. I checked the jdk api but couldn't find any such methods. Is there any method that I can make use of?. Any help would much appreciated.
Take a look at java.util.regex package Matcher and Pattern classes:
http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html
Just use the Java regular expression API
Pattern pat = Pattern.compile("\\d");
Matcher mat= pat.matcher("Foo99Bar66Baz");
while(mat.find()) {
System.out.println(mat.group());
}
You can find simple but quite comprehensive examples for startup in the following link
http://www.vogella.de/articles/JavaRegularExpressions/article.html
Also Pattern and Matcher usage example in:
http://www.vogella.de/articles/JavaRegularExpressions/article.html#regexjava

How to match a set of string in regexp

How do I combine ch..+ and ch..- in regexp effectively without having to scan separately?
And are we using matcher in the pattern?
My output code is like this:
ch01+
ch01-
ch02+
ch02-
...
How do I combine ch..+ and ch..- in regexp effectively without having to scan separately?
Use | (pipe) for alternation:
ch..(\+|-)
And are we using matcher in the pattern?
Depends on how you're using the regexp and the pattern. To get a concrete answer, you'll have to show some actual code, or ask a much more specific question.
N.B. If you want to restrict the two characters after ch to 0-9, you can use \d, which is a shorthand character class for [0-9]:
ch\d{2}(\+|-)
You can use a character class containing just "+" and "-" like so "[+-]".
Pattern p = Pattern.compile("ch..[+-]");
Matcher m = p.matcher("ch01+");
if (m.find()) {
// found it...

Categories