0

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.

jm90
  • 137
  • 11
  • 1
    There's no Groovy "or" operator here--regexes have their own grammar. – Dave Newton Jun 27 '23 at 17:32
  • The single- or double-pipe/-ampersand regards logical vs bitwise operators; which one to use depends on context/requirements. – Dave Newton Jun 27 '23 at 17:39
  • re: the edit, see https://stackoverflow.com/q/1724205/438992 – Dave Newton Jun 27 '23 at 17:43
  • @dave-newton. Thanks for the clarification regarding regex. I thought that the regex here was handled as Groovy since it was in Groovy-code but a single pipe works with regex itself for example when tested with regex101.com. However, I am not sure if I understand bitwise right but is so that bitwise can be applied for a string? if so then maybe that explains why those examples in the post work. – jm90 Jun 27 '23 at 17:43
  • 1
    The regex is in a string; it's not even a regex until it starts being parsed. – Dave Newton Jun 27 '23 at 17:45
  • After all if I have understood right a bitwise should not be used with string? I really don't understand why it works with the "City" example of my post edit. The original regex code example part makes sense as there the or operator was not handled in Groovy so a normal regex-syntax was applied. – jm90 Jun 27 '23 at 18:50
  • read this https://stackoverflow.com/questions/5911063/how-boolean-values-are-treated-in-java-by-bit-wise-operators for using bitwise operators on booleans. – injecteer Jun 29 '23 at 10:56
  • @injecteer. Thanks. In that link it was mentioned that "There are no bitwise operations on `boolean` in Java" and that " `&` and `|` don't do bitwise operations in Java, but logical operations" . I assume this is same for `string` data type as I have two strings `London` and `New York` in that second code example? – jm90 Jun 30 '23 at 17:52

0 Answers0