You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any way StringScanner could keep ^ meaningful? As it is, it denotes the current the beginning of the rest, but not whether we're at the start of a line.
In the following example, "bar" is not expected to be stripped.
$ cat /tmp/foo.rb
require 'strscan'
def strip(str)
res = +''
s = StringScanner.new(str)
while !s.eos?
res <<
if s.scan(/foo/)
''
elsif s.scan(/^bar/)
''
else
s.scan(/./)
s.matched
end
end
res
end
p strip('foobarbaz')
$ ruby /tmp/foo.rb
"baz"
If there's no way for StringScanner to "fix" the ^ anchor, the documentation should warn about that.
Cheers!
I totally missed it... Thanks, it does solve my issue!
However the suggestion remains: there's no way to have ^ mean what it means?
And I think a warning (plus a reference to beginning_of_line?) would help.
I think that there is no way to detect ^ from a Regexp object...
I agree on this. Not to mention that the anchor can be anywhere in the Regexp, not just at its start. And maybe in an optional part, or an alternation.
I was rather thinking of a way to tell the RE engine that it is not at a beginning of line so ^ should not match.
I don't know its internals well enough, but that path seems way safer and exact to me rather than trying to massage the regex.
Activity
kou commentedon May 13, 2024
How about using
beginning_of_line??akimd commentedon May 13, 2024
I totally missed it... Thanks, it does solve my issue!
However the suggestion remains: there's no way to have
^mean what it means?And I think a warning (plus a reference to
beginning_of_line?) would help.Thanks a lot for the super fast answer.
kou commentedon May 13, 2024
Right.
I think that there is no way to detect
^from aRegexpobject...akimd commentedon May 13, 2024
I agree on this. Not to mention that the anchor can be anywhere in the Regexp, not just at its start. And maybe in an optional part, or an alternation.
I was rather thinking of a way to tell the RE engine that it is not at a beginning of line so
^should not match.I don't know its internals well enough, but that path seems way safer and exact to me rather than trying to massage the regex.
akimd commentedon May 13, 2024
To be clear: I was referring to the documentation, not a warning from strscan.
kou commentedon May 13, 2024
Ah, sorry. I misunderstood.
Could you open a PR to add a warning document?