in reply to Re: Using pos() inside regexp (no /e)
in thread Using pos() inside regexp

Ups, your solution is much better than mine, thanks a large. There is a small problem with last line in that case so i think better to use something like
s/((?:[^\n]{0,15}$)|(?:[^\n]{0,14},)|(?:[^\n]{15})(?=[^\n]))/$1\n/g;
nevertheless it's interesting to know is it possible to use pos() function to set search position inside of regexp, or it works outside only.

Replies are listed 'Best First'.
Re^3: Using pos() inside regexp (no /e)
by ikegami (Patriarch) on Oct 08, 2010 at 15:32 UTC
    If I'm reading that correctly, you want a newline at the end? If so, the following will do:
    # Adds trailing newline s/([^\n]{0,14},|[^\n]{15})(?!\n)/$1\n/g
    Compare with tye's:
    # Doesn't add trailing newline s/([^\n]{0,14},|[^\n]{15})(?=[^\n])/$1\n/g

      Did you try your solution? It doesn't add a trailing newline (nor fix the more important problem). Here's my next stab:

      s{( (?:[^\n]{1,15})(?=\n|$) # Line requiring no wrapping | (?:[^\n]{0,14},)(?!\n) # Line that can be wrapped at comma | (?:[^\n]{15})(?!\n) # Line to be wrapped, not at comma )\n? }{$1\n}gx;

      And just two test cases:

      (update) Note there are three ways to write part of that: (?=\n|$) or (?![^\n]) or (?m:$). The last can be written as just $ by putting the m option on the end of the s/// construct. I leave the choice up to you.

      - tye        

        You are my hero :)
        Yes, i've tried my solution, and extra new line at the end is not a big problem.
        But \n in the middle of the initial string is a possible situation, so thanks a large for you attention!
Re^3: Using pos() inside regexp (no /e)
by andal (Hermit) on Oct 08, 2010 at 15:32 UTC
    No. The pos() works only outside of matching. It works with the position where the matching operation has stopped. During matching this function does not make sense. \G references the position from the previous matching.

      The pos() works only outside of matching.

      Except when it works inside of matching ;)

      $ perl -le'() = "abcdef" =~ /..(?{ print pos() })/g;' 2 4 6
        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.
      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?
        1. perldoc perlretut (and company)
        2. Tutorials
        3. "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.