the meaning of [ ] in a regular expression [- ] ?

Asked 1 years ago, Updated 1 years ago, 68 views

in Regular Expression (Ruby)
^ISBN[-]?[ dd|-]+

For

^ISBN-?[ dd|-]+

But I think the result will be the same
In this case, [] means [] to match any of the characters specified in [] and is different from [] in classes.

ruby regular-expression

2022-09-30 21:32

1 Answers

Yes, Ruby's regular expression [-] matches one character and is equivalent to the regular expression -.

Although not explicitly written in the Ruby 2.5 document (this or this), hyphens in [] are treated as expressing a range only when [a-z].Nonmetacharacters without backslash escape are treated as nonmetacharacters, such as when at the beginning.

For example, the regular expression [-abc] matches -, a, b, and c (Example: Rubular/"https://wandbox.org/permlink/jrOb0pT4Kk6UszvV"rel="norever/norebox>.

Reference Blog: Regular Expressions: Symbols that do not need to be escaped within character class [] --Tech Racho


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.