Pipe Symbol in regex | (Alteration)

  • You can use alteration or pipe symbol to find out pattern out of several regular expression.
  • Pipe symbol works just like OR operator in java language.
  • If you want to search bikes or cars using same regex then you can separate bikes and cars token with a pipe symbol and search both of them together.
  • Text : Tonight Bikes and Cars are going to have a race on highway
    Find : Text “Bikes” and “Cars” in the same regex.
    Regex : Bikes|Cars
  • Now suppose you want to add additional regex on the search token like \b then you need to group the search using round brackets to do it
    Example : \b(Bikes|Cars)\b
  • If we had omitted the round brackets, the regex engine would have searched for a word boundary followed by Bikes, or, Cars followed by a word boundary.

Leave a Comment