in reply to Re^3: How to convert a string with regex to a string with a fixed length?
in thread How to convert a string with regex to a string with a fixed length?

Here a extract of the manual: How do I specify arbitrary text substitution? Enter text in the Regular expression: text box. The syntax for the string is: /search/replace/g where search is the string to search for and replace is the string to replace search with. By default, only the first occurrence of search is replaced. To replace all instances, add g at the end of the command. For example, if the reader decodes this string: UPC 05214275600 You could replace the string "UPC" with the string "SKU CODE:" by using this substitution string: /UPC/SKU CODE:/ which would produce this output string: SKU CODE: 05214275600 You can enter multiple susbstitution pairs separated by a space. Multiple pairs are used sequentially: the result of the first pair is used as input for the second pair, and so on. AND ALSO: How do I use a wildcard to match different characters or patterns? You can match any single character, any character from a set of characters, any character from a range of characters, or any character that is not within a range of characters using these special characters in the search string: . Match any single character abcd... Match any single character from the set a, b, c, d ... a-z Match any single character within the range of characters a through z (letters or numbers can be used) To invert the sense of a wildcard using a set or range of characters, precede the characters with a ^ character. To match a variable number of characters, append one of the following modifier characters to the character or wildcard being matched:. + matches one or more of the preceding character * matches zero or more of the preceding character ? matches zero or one of the preceding character {n} matches n instances of the preceding character {n,m} matches at least n but not more than m instances of the preceding character {n,} matches at least n instances of the preceding character Here are some examples of wildcard matching: /A.B/AB/g removes any single character between 'A' and 'B' in the input string /A.*B/AB/g removes any number of characters from between 'A' and 'B' in the input string
  • Comment on Re^5: How to convert a string with regex to a string with a fixed length?

Replies are listed 'Best First'.
Re^6: How to convert a string with regex to a string with a fixed length?
by moritz (Cardinal) on Jul 29, 2008 at 17:08 UTC
    Obviously you have to remove the leading s of s/... then. If that still does not work, you can use a really ugly hack by making a regex for each possible string length:
    /\b(\d)/${1}000000000000/ /\b(\d{2})/${1}00000000000/ /\b(\d{3})/${1}0000000000/ /\b(\d{4})/${1}000000000/ /\b(\d{5})/${1}00000000/ ...

    That still assumes that the RHS is actually interpolated by your misterious program. If it's not, you could still try to work with look-behinds:

    /(?<=\b\d)$/000000000000/ /(?<=\b\d{2})$/00000000000/ /(?<=\b\d{3})$/0000000000/ /(?<=\b\d{4})$/000000000/ /(?<=\b\d{5})$/00000000/ /(?<=\b\d{6})$/0000000/ ...

    (not tested)

Re^6: How to convert a string with regex to a string with a fixed length?
by ambrus (Abbot) on Jul 29, 2008 at 17:14 UTC

    Well, try something like

    s/$/ + /; s/^\(.\{1,100\}\).*/\1/;

    The first substitution has 100 or more spaces on the rhs.

Re^6: How to convert a string with regex to a string with a fixed length?
by runrig (Abbot) on Jul 29, 2008 at 17:06 UTC
    try:
    s/(\d+)/sprintf("%013d", $1)/eg
    Update: that's what I get for not reading the OP carefully... how about (and being slow...oh well):
    s/(\d+)/reverse(sprintf("%013d", scalar(reverse($1))))/eg;

      Padding is on the wrong side.

      --MidLifeXis