I have this simple example with Groovy and regex.
So here it is expected by regex that three or four characters are found from the string and in case of those a match is made.
String name = "ABC"
// Match is made if three or four characters are found in the string
def match = name =~ "\\w{3}|\\w{4}"
if (match.matches()) {
println ("Match found!")
} else {
println ("No match!")
}
My question concerns "or" operator. From Groovy documentation I found ||
as "or" and used that first but then noticed by running the code that also a single |
pipe worked like in this code example.
I am wondering is that also correct syntax-wise or should I use two ||
pipes for "or"?
From the documentation I also found a reference to single pipe |
as bitwise "or" but it was mentioned that bitwise can be applied for byte
, short
, int
, long
, or BigInteger
so isn't string
outside of those types?
I also noticed same for "and" operator so that both &&
or &
worked.
So as a conclusion I am not currently sure if that single character |
for "or" just somehow worked but at the same time did not return an error or was this also a correct syntax to use.
Edit: Same thing seems to apply if regex is taken away and only Groovy operator "or" tested:
String city = "London"
if (city == "London" | city == "New York") {
println ("One of cities matched!")
} else {
println ("No city match found")
}
With this also |
or ||
both worked.