1

I'm having trouble understanding the differences between square brackets [] and the vertical bar | in Regular Expressions.

For example: When I use this code in java to change the format of a String (Using |):

str = str.replaceAll("(\\d+|\\W)", "$0 ").replaceAll("\\s+", " ").trim();

From: (12+((23+ 37) * (774*435)))

To: ( 12 + ( ( 23 + 37 ) * ( 774 * 435 ) ) )

Which is the desired output.

However, when I use this code, I get a different output (Using []):

str = str.replaceAll("[\\d+\\W]", "$0 ").replaceAll("\\s+", " ").trim();

I get from: (12+((23+ 37) * (774*435)))

To: ( 1 2 + ( ( 2 3 + 3 7 ) * ( 7 7 4 * 4 3 5 ) ) )

Notice the spaces between the numbers (not desired).

So, can somebody please explain what is going on? Or what is the difference between [] and |?

Thank you.

Jesus
  • 31
  • 3
  • Check out this link it's already answered Click [here](http://stackoverflow.com/questions/9801630/what-is-the-difference-between-square-brackets-and-parentheses-in-a-regex ""). – chuck Oct 25 '14 at 18:24
  • `|` is used to provide 'or' functionality when used within ()'s, while the `[]` denote character classes, as in match any of the characters/symbols inside the `[]` for the specified number of characters denoted by the following `?*+{#}` specifiers, or if a `^` is used, it means __don't__ match that symbol. – Ryan J Oct 25 '14 at 18:24

2 Answers2

2

Here is a similar question.

The vertical bar is a regex "or" means "a or b"

Square brackets are a character class meaning "any character from a or b.

Character class is a shorthand for "or". From this explanation

  • If you want to match an a or an e use [ae]
  • A character class matches only a single character.
  • You can also use hyphen to specify a range such as [0-9] or [a-e]
Community
  • 1
  • 1
Wade Anderson
  • 2,173
  • 1
  • 16
  • 20
1

The brakcets ([]) negate the effect of the +. So when you have [\\d+\\W], that matches a single character (digit, + or non-word)

Chris Dodd
  • 101,438
  • 11
  • 111
  • 197