The pos() works only outside of matching.
Except when it works inside of matching ;)
$ perl -le'() = "abcdef" =~ /..(?{ print pos() })/g;'
2
4
6
| [reply] [d/l] |
Well, I guess here we talk about 2 different "pos()" functions. The one that is documented under "perldoc -f pos" allows modification of the position where the match has ended. The one you have mentioned, and the one that is used in substitute part of s/// are read-only functions. They can not be used to modify the position. Just the name of the function is the same, but functionality is different.
| [reply] |
There isn't two functions with the same name. (That doesn't even make sense.) It's not read-only.
s/// uses the specified position when it starts matching (not in the middle), and only for \G. As mentioned by someone else, it makes no sense to modify the pos s/// uses in the middle of its operation.
If you want to move pos back, you would want to the input with one thing, then replace it again with something else. Replace it with the right thing the first time.
If you want to move pos forward, s/// has no way to know what you want to do with the chunk you're skipping over. Should it be copied? Should it be replaced?
| [reply] [d/l] |
hmm i can read pos() value during matching, so why can't i set it? Sence of this action is deep control of regexp actions.
And about \G, i have no idea how can \G assertion help me to make work regexp from first post:
$data =~s/([^\n]{16})/ch($1)/ge;
Could you, explain how it can help me? | [reply] [d/l] |
- perldoc perlretut (and company)
- Tutorials
- "Mastering Regular Expressions," Friedl (O'Reilly)
which is a semi-nice way of saying "RTFM."
Really!
Once you learn to fish in Perl's documentation pond, you'll never go hungry.
| [reply] [d/l] |
I'm sorry, but how that can help me?
I've read 1 and 3 points of your list, and just take a look at second point. They don't contain answers on my questions, which is:
1. Is it possible to set pos() value from regexp itself, and how to do it?
2. How can \G assertion help me to change pos() value?
Look like you have misunderstand me.
perldoc -f pos
pos SCALAR
pos Returns the offset of where the last "m//g" search left
+ off for
the variable in question ($_ is used when the variable
+is not
specified). Note that 0 is a valid match offset. "und
+ef"
indicates that the search position is reset (usually du
+e to
match failure, but can also be because no match has yet
+ been
performed on the scalar). "pos" directly accesses the l
+ocation
used by the regexp engine to store the offset, so assig
+ning to
"pos" will change that offset, and so will also influen
+ce the
"\G" zero-width assertion in regular expressions. Becau
+se a
failed "m//gc" match doesn't reset the offset, the retu
+rn from
"pos" won't change either in this case. See perlre and
+ perlop.
Point is “"pos" directly accesses the location used by the regexp engine to store the offset” and I want to change it.
Second point is
perl -e "\$_='qwerty';s/r/print pos()/e;"
so I can read pos value, and it is logical that I can set it too. But I don't know how to do it, and can't find any examples.
Now about \G which says regexp to start next search from the end of previous. It's main purpose to check several regexp on string, special for that its used with c modifier practically all the time. But in my case all actions make in function which called by regexp. But after then function change string, new search must start from earlier position then pos() value. And I don't understand how can \G help me?
Of course I can miss something, but what?
I'm sorry for my language level, i can't says clearly what exactly I mean. But hope you will understand.
| [reply] [d/l] [select] |