in reply to How to convert a string with regex to a string with a fixed length?

If you can use an arbitrary substitution expression with your regex, try this:
s/\b(\d+)\b/sprintf "%013d", $1/eg;

If not, please tell us exactly what you can use - a single regular expression only matches stuff, but can't change the source string.

Update: I missed that the 0's should actually be appended at the end. You can try to use lodin's solution inside the substitution part, though. Or to rescue my original solution:

s/\b(\d+)\b/reverse sprintf "%013d", scalar reverse $1/eg;

Replies are listed 'Best First'.
Re^2: How to convert a string with regex to a string with a fixed length?
by MrQBRIDGE (Initiate) on Jul 29, 2008 at 16:44 UTC
    this is not working by me.
      This is not very helpful.

      Would you care to elaborate? In what way does it not work? Wrong output? if yes, which? Or does it raise an error? if so, which?

        The scanner gives me the original scanned barcode without changing anything. But no errors at all. Is your solution s/\b(\d+)\b/reverse sprintf "%013d", scalar reverse $1/eg; a real regex? or has it already some extra program-lines? (like sprintf...)
        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