Bug #19273
closed
[Regexp] regexp does not match expected
Added by taichi730 (Taichi Ishitani) almost 2 years ago.
Updated almost 2 years ago.
Description
A regexp pattern does not match expected with Ruby 3.2.
Ruby 3.2
pattern = /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))){0,3}/
"10:0:0".mathc(pattern) #=> #<MatchData "10">
"0:0:0".match(pattern) #=> #<MatchData "0:0:0">
String "10.0.0" is only partially matched but not all of it.
Ruby 3.1.0:
pattern = /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))){0,3}/
"10:0:0".match(pattern) #=> #<MatchData "10:0:0">
"0:0:0".match(pattern) #=> #<MatchData "0:0:0">
Both strings matches with the regexp wholy.
- Assignee set to make_now_just (Hiroya Fujinami)
This pattern shows a binary|decimal|hexadecimal value splitted by : character.
decimal only and decimal|hexadecimal patterns match expectedly.
decimal only pattern:
irb(main):019:0> pattern = /(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)(?::(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)){0,3}/
=> /(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)(?::(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)){0,3}/
irb(main):020:0> "10:0:0".match(pattern)
=> #<MatchData "10:0:0">
decimal|hex pattern:
irb(main):021:0> = /(?-mix:(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?-mix:[+-]?(?:[1-irb(main):021:0> pattern = /(?-mix:(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))){0,3}/
=> /(?-mix:(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?-mix:[+-]?(?:[1-9]_?(?:\d_?...
irb(main):022:0> "10:0:0".match(pattern)
=> #<MatchData "10:0:0">
But binary|decimal pattern does not.
irb(main):023:0> pattern = /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d))(?::(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d))){0,3}/
=> /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d))(?::(?-mix:(?i-mx:[+-]?0b[01](?:_?[0...
irb(main):024:0> "10:0:0".match(pattern)
=> #<MatchData "10">
Pattern using *
quantifier instead of {0,3}
quantifier matches expactedly.
irb(main):027:0> pattern = /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*)))*/
=> /(?-mix:(?i-mx:[+-]?0b[01](?:_?[01])*)|(?-mix:[+-]?(?:[1-9]_?(?:\d_?)*)?\d)|(?i-mx:[+-]?0x\h(?:_?\h)*))(?::(?-mi...
irb(main):028:0> "10:0:0".match(pattern)
=> #<MatchData "10:0:0">
- Subject changed from [Regexp] regexp does not does not match expected to [Regexp] regexp does not match expected
PR #7035 for fixing the bug is created.
- Status changed from Open to Closed
- Backport changed from 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN to 2.7: DONTNEED, 3.0: DONTNEED, 3.1: DONTNEED, 3.2: REQUIRED
merged at b726d60c986bf951d13e7a2ab5f5e58f58657b03
- Backport changed from 2.7: DONTNEED, 3.0: DONTNEED, 3.1: DONTNEED, 3.2: REQUIRED to 2.7: DONTNEED, 3.0: DONTNEED, 3.1: DONTNEED, 3.2: DONE
ruby_3_2 a7d467a792c644a7260d6560ea2002fdb8ff6de3 merged revision(s) b726d60c986bf951d13e7a2ab5f5e58f58657b03.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like1Like0Like0