Multiply all numbers in a string

19

Winner: Chilemagic, a massive 21 bytes!

You may continue to submit your answer, however you can no longer win. Original post kept:


Your goal is to find all numbers in a string, and multiply each one individually by a value input by the user

  • You will not need to worry about decimals
  • The user will input a number and a string
  • The user must type the number and string at some point, however the method the program reads it does not matter. It can be with stdin, reading a text file, etc, however the user must press the 9 button on their keyboard (for example) at some point
  • Anything that can compile and run is acceptable

Example:

Sentence input: This 1 is22a 3352sentence 50

Number input: 3

Output: This 3 is66a 10056sentence 150


  • This contest ends on September 6th, 2014 (7 days from posting).
  • This is a , so the shortest code wins

Jon

Posted 2014-08-31T04:33:56.993

Reputation: 1 505

Any number is to be multiplied. I updated my example. – Jon – 2014-08-31T04:59:18.023

I don't understand the significance or use of "press the 9 button" in your challenge. How or where does it apply in the example given? – Darren Stone – 2014-08-31T05:01:57.217

If the number to be multiplied by is 9, the user must press the 9 key at some point. – Jon – 2014-08-31T05:08:25.260

3@Darren He's basically saying that you can't hardcode the number. – Beta Decay – 2014-08-31T08:59:36.903

2Can we make assumptions about the size of the numbers involved, particularly the possibility of overflow? Depending on the results of the above, do we treat "-1234" as "-" followed by 1234 or as -1234? – Alchymist – 2014-09-01T11:30:55.437

Is it acceptable to output the number 0 as an empty string? I assume not, but I figured I should make sure. – Ilmari Karonen – 2014-09-01T19:59:15.947

@Alchymist There isn't any difference between these two cases if the factor is positive. – FUZxxl – 2014-09-01T22:42:06.310

What are the restrictions if any on what programming language can be used? – x-code – 2014-09-02T00:29:20.157

@x-code Any language. – Jon – 2014-09-02T04:23:08.693

1To add to @IlmariKaronen's question: What to do with leading zeroes as in "Bond is agent 007" -> "Bond is agent 21" or "Bond is agent 0021" or "Bond is agent 021" or ...? – Hagen von Eitzen – 2014-09-02T11:35:39.447

agent 007 should give agent 025, because, you see, 007 is octal. :-) – Florian F – 2014-09-04T12:58:35.053

@FUZxxl There is a difference if there can be numeric overflow – Alchymist – 2014-09-08T12:33:30.303

@Alchymist That's a point. We can assume by common code-golf rules that the input data will only contain numbers of reasonable size. – FUZxxl – 2014-09-09T08:36:03.987

Answers

24

Update - Perl - 17

s/\d+/$&*$^I/ge

15 characters + 2 for -i and -p flags.

We can use the -i flag to input a file extension, but since we aren't reading any files, we can use it to get the number and it the variable $^I will get assigned to it.

Run with:

perl -e'print"This 1 is22a 3352sentence 50"' | perl -i3 -pe's/\d+/$&*$^I/ge'

Perl - 21

Updated as per @Dennis's comment.

$n=<>;s/\d+/$&*$n/ge

Run with -p flag.

Example run:

perl -e'print"This 1 is22a 3352sentence 50\n3"' | perl -pe'$n=<>;s/\d+/$&*$n/ge'

Explanation:

$n=<>; read in the number

-p prints the output

s/\d+/$&*$n/ge Read the input with <> and search for one or more digits and replace them with the digits times the number. g is global, e is eval the replace potion of the s///. $& contains what was matched and it multiplied by the number, $n.

You can read more about s/// in perlop and more about Perl regexes in perlre.

Another solution:

@F.Hauri pointed out you can also use the s switch to assign the $n variable to 4. I'm not sure how many characters this counts as but I'll leave it here:

perl -spe 's/\d+/$&*$n/ge' -- -n=4 <<<'This 1 is22a 3352sentence 50'

hmatt1

Posted 2014-08-31T04:33:56.993

Reputation: 3 356

I think you could save a few more characters by using <>=~ instead of _$=<>;. Also could you please explain what the s part of the regex means, for us novices? – Tal – 2014-08-31T05:26:27.337

@Tal String.​​​ – Kaz Wolfe – 2014-08-31T07:23:48.890

3@Mew I'm pretty sure it's for "substitution" – Martin Ender – 2014-08-31T08:53:04.257

@MartinBüttner Probably. I need to learn perl. I only ever saw it used on strings, so that's where I picked that up. – Kaz Wolfe – 2014-08-31T08:53:55.577

It is for "substitution". It can also be m, which stands for "match". – Ingo Bürk – 2014-08-31T10:49:14.053

You don't need the trailing semicolon, that'll save you a character – user0721090601 – 2014-08-31T11:06:59.893

Right tool for the task. – Johannes Kuhn – 2014-08-31T13:01:16.397

1>

  • If you use $& instead of $1, you can shorten (\d) to \d. 2. If you use the -p switch and change the input order, you can drop say<>=~ and r.
  • < – Dennis – 2014-08-31T14:59:49.987

    Changing the inner $n to ($n.=<>) saves a byte. – primo – 2014-08-31T15:17:24.090

    @primo I think that is the same length. – hmatt1 – 2014-08-31T15:41:56.537

    @chilemagic indeed, I miscounted. – primo – 2014-09-01T05:31:00.457

    1You may use -s switch, to whipe $n=<> out: perl -spe 's/\d+/$&*$n/ge' -- -n=4 <<<'This 1 is22a 3352sentence 50' (do render: This 4 is88a 13408sentence 200) – F. Hauri – 2014-09-01T15:32:15.243

    9

    JavaScript (ES6) - 48 44 chars

    Thanks to @bebe for saving one character. Update: 8/Mar/16, removed another four characters

    b=(p=prompt)();p(p().replace(/\d+/g,y=>y*b))
    

    Ungolfed:

    var sentence = prompt(),
        num = parseInt(prompt(), 10); // base 10
    
    sentence = sentence.replace(/\d+/g, digit => digit * num);
    
    alert(sentence);
    

    43 chars:

    (p=prompt)(p(b=p()).replace(/\d+/g,y=>y*b))
    

    b=(p=prompt)();p(p().replace(/\d+/g,y=>y*b))
    

    Requires number input first and then sentence. Cut one char more here thanks @bebe again!

    Gaurang Tandon

    Posted 2014-08-31T04:33:56.993

    Reputation: 837

    you don't need to parse number input – bebe – 2014-08-31T15:44:11.700

    @bebe Of course! I didn't notice! Thanks a lot! – Gaurang Tandon – 2014-09-01T05:25:21.057

    (p=prompt)(p(b=p()).replace(/\d+/g,y=>y*b)) another one (but this asks for multiplier first) – bebe – 2014-09-01T07:19:08.993

    @bebe Thanks for this too! – Gaurang Tandon – 2014-09-01T13:49:13.060

    6

    Python 2 (79)

    import re
    n=input()
    s=input()
    print re.sub('\d+',lambda x:`int(x.group())*n`,s)
    

    Sample run

    Input:
    $ python mult.py
    3
    "This 1 is22a 3352sentence 50"
    
    Output:
    This 3 is66a 10056sentence 150
    

    Online demo: http://ideone.com/V6jpyQ

    Cristian Lupascu

    Posted 2014-08-31T04:33:56.993

    Reputation: 8 369

    Brilliant. I guess re.sub is the method Beta Decay and I (the other two Python submissions) were trying to reimplement. It would have been so easy... Something else that I've learned! ;) – Falko – 2014-09-01T10:19:18.250

    @Falko re.sub aside, you've done an excellent job at golfing the string replace logic – Cristian Lupascu – 2014-09-01T10:27:22.077

    @Falko I agree, re.sub is exactly the thing I wanted! – Beta Decay – 2014-09-02T15:43:51.057

    4

    Python 2 - 126

    import re
    n=input()
    s=input()
    for i in list(re.finditer('\d+',s))[::-1]:s=s[:i.start()]+`int(i.group())*n`+s[i.end():]
    print s
    

    First input: integer n.

    Second input: string s (with quotation marks, e.g. "abc42").

    Falko

    Posted 2014-08-31T04:33:56.993

    Reputation: 5 307

    4

    CJam, 47 33 30 bytes

    q_A,sNerN%\[_A,s-Ner~](f*]zs1>
    

    Reads the number and the string (in that order and separated by a single space) from STDIN.

    Try it online.

    Example run

    $ cjam multiply.cjam <<< '7 N0 R3GUL4R 3XPR35510N5 1N CJ4M M4K3 M3 4 54D P4ND4'
    N0 R21GUL28R 21XPR248570N35 7N CJ28M M28K21 M21 28 378D P28ND28
    

    How it works

    q                                 " Read from STDIN (“Q”) and push a second copy.         ";
      A,s                             " Push “0123456789” (thanks, @aditsu).                  ";
     _   NerN%                        " Replace digits with linefeeds and split at linefeeds. ";
              \ _A,s-Ner              " Replace non-digits with linefeeds.                    ";
                        ~             " Evaluate the resulting string.                        ";
               [         ]            " Collect the results in an array.                      ";
                          (f*         " Multiply all other integers by the first.             ";
                             ]z       " Interleave the elements of both arrays.               ";
                               s1>    " Flatten the result and discard the leading space.     ";
    

    Dennis

    Posted 2014-08-31T04:33:56.993

    Reputation: 196 637

    This is the weirdest piece of code ever!!! – azerafati – 2014-08-31T16:05:24.863

    1

    @Bludream: This isn't even the weirdest code I've written. :P

    – Dennis – 2014-08-31T16:43:54.567

    Hmm, 3 * 7 = 27? – aditsu quit because SE is EVIL – 2014-08-31T20:41:06.987

    lol, yeah voted for both. Though These are not going to make world a better place. How on earth programming languages who are supposed to be readable turned into this?? – azerafati – 2014-09-01T07:26:31.970

    @aditsu: Splitting vs iterating, the battle continues. :P – Dennis – 2014-09-01T14:34:52.800

    Wow, impressive.. and you didn't even use my A,s to get the digits – aditsu quit because SE is EVIL – 2014-09-01T20:29:19.477

    @aditsu: Nice trick. I skipped that part when I read your answer. Can I borrow it? – Dennis – 2014-09-01T20:35:55.497

    No need to ask :p – aditsu quit because SE is EVIL – 2014-09-01T20:59:47.590

    4

    Bash+coreutils, 38 bytes

    eval echo `sed "s/[0-9]\+/$\[$1*&]/g"`
    

    Reads input string from STDIN and multiplier as a command-line parameter.

    Output:

    $ ./multstr.sh 3 <<< "This 1 is22a 3352sentence 50"
    This 3 is66a 10056sentence 150
    $ 
    

    Digital Trauma

    Posted 2014-08-31T04:33:56.993

    Reputation: 64 644

    6Interesting idea, but this will work only if the string doesn't any contain characters that are special to Bash... This is an example of a string I wouldn't try: 1 D4R3 Y0U: ; rm -rf / – Dennis – 2014-09-01T06:22:34.623

    @Dennis yes that is a rather unfortunate caveat – Digital Trauma – 2014-09-01T15:04:24.927

    3

    C# in LINQPad, 124

    Straightforward. Please use CTRL+2 in LINQPad (Language: C# Statements).

    var s=Console.ReadLine();var k=int.Parse(Console.ReadLine());new Regex("\\d+").Replace(s,t=>""+int.Parse(t.Value)*k).Dump();
    

    If the multiplier is given as first input parameter, it can be done in 116 characters:

    var k=int.Parse(Console.ReadLine());new Regex("\\d+").Replace(Console.ReadLine(),t=>""+int.Parse(t.Value)*k).Dump();
    

    EDIT:

    Thanks to Abbas's comment below, this can even be golfed more by using Regex static method, rather than instantiate it:

    var k=int.Parse(Console.ReadLine());Regex.Replace(Console.ReadLine(),"\\d+",t=>""+int‌​.Parse(t.Value)*k).Dump();
    

    Jacob

    Posted 2014-08-31T04:33:56.993

    Reputation: 1 582

    Nice one, code-golf in C# isn't easy! I don't want to get rep on your effort so I'll provide you a tip instead of posting my own answer: use the static Regex.Replace(string, string, string) instead of new Regex("...").Replace(...); Here's the short multiplier-first version: var k=int.Parse(Console.ReadLine());Regex.Replace(Console.ReadLine(),"\\d+",t=>""+int.Parse(t.Value)*k).Dump();. In both versions this saves another 5 characters, getting you on 119 for the long version and 111 for the multiplier-first version

    – Abbas – 2014-09-01T14:23:17.770

    1Thanks for the tip, however, I decided not to change my original answer, such that others will be able to learn what-not-to-do from me, and what-to-do from you - regarding C# Regex Golfing. – Jacob – 2014-09-01T14:29:51.197

    I understand you but you could also add my tip in your answer as an edit. Users tend to read this more than they will a list of comments with tips. – Abbas – 2014-09-01T14:32:19.663

    Sure. The answer was edited. – Jacob – 2014-09-01T14:36:05.577

    2

    Mathematica 71 61

    With 10 chars saved thanks to Martin Buttner.

    Spaces in code are for readability.

    f is a function in which s is the input string and n is the number to multiply discovered string numbers by.

     StringReplace[#, a : DigitCharacter .. :> ToString[#2 FromDigits@a]] &
    

    Examples

     s="This 1 is22a 3352sentence 50"
    

    Integer

    StringReplace[#, a : DigitCharacter .. :> ToString[#2 FromDigits@a]] &@@{s, 3}
    

    "This 3 is66a 10056sentence 150"


    Rational number

    StringReplace[#, a : DigitCharacter .. :> ToString[#2 FromDigits@a]] &@@{s, -7.13}
    

    "This -7.13 is-156.86a -23899.8sentence -356.5"


    Complex Number

    StringReplace[#, a : DigitCharacter .. :> ToString[#2 FromDigits@a]] &@@{s, -5 + 3 I}
    

    "This -5 + 3 I is-110 + 66 Ia -16760 + 10056 Isentence -250 + 150 I"

    DavidC

    Posted 2014-08-31T04:33:56.993

    Reputation: 24 524

    1I don't know Mathematica. But in your examples all numbers and words are separated by spaces. Does it work for numbers directly attached to letters as well, like "abc42"? – Falko – 2014-08-31T09:45:59.300

    For StringReplace it makes no difference whether there are spaces. I used the examples originally given by Chiperyman. – DavidC – 2014-08-31T15:55:19.917

    I switched to the updated example. (I had used the example given earlier by Chiperyman.) For StringReplace it makes no difference whether there are spaces. – DavidC – 2014-08-31T16:04:30.027

    2

    Cobra - 171

    use System.Text.RegularExpressions
    class P
        def main
            a=int.parse(Console.readLine?'')
            print Regex.replace(Console.readLine,'\d+',do(m as Match)="[int.parse('[m]')*a]")
    

    Οurous

    Posted 2014-08-31T04:33:56.993

    Reputation: 7 916

    2

    Python 3 - 141

    I don't think I can golf this any more...

    import re
    n=input()
    f=input()
    o=''
    p=0
    for i in re.finditer('\d+',f):o+=f[p:i.start()]+str(int(i.group())*int(n));p=i.end()
    o+=f[p:]
    print(o)
    

    Example:

    3     # This is input
    h3110 # This is input
    h9330 # This is output
    
    10
    hello100 hello100
    hello1000 hello1000
    

    Beta Decay

    Posted 2014-08-31T04:33:56.993

    Reputation: 21 478

    2Unfortunately this approach does not work. For n=2 and s="1 2" it yields 4 4, since replace modifies the first number twice. That's the same problem I'm facing right now with Python 2... ;) – Falko – 2014-08-31T07:45:54.053

    @Falko I've managed to fix the problem, although making my code longer in the process – Beta Decay – 2014-08-31T08:57:42.707

    Oh, well done! Just some minor remarks: m=input() saves you nothing. And x=int is actually 2 bytes longer than calling int(...) twice. – Falko – 2014-08-31T12:16:46.637

    2

    Ruby 40

    a,b=$*
    p a.gsub(/\d+/){|s|s.to_i*b.to_i}
    

    Input from stdin.

    Example run:

    $ ruby mult.rb "This 1 is22a 3352sentence 50" 3 
    "This 3 is66a 10056sentence 150"
    

    Online demo: http://ideone.com/4BiHC8

    Cristian Lupascu

    Posted 2014-08-31T04:33:56.993

    Reputation: 8 369

    2

    Lua: 73 69 characters

    r=io.read
    s=r()n=r()s=s:gsub("%d+",function(m)return m*n end)print(s)
    

    Sample run:

    bash-4.3$ lua -e 'r=io.read;s=r()n=r()s=s:gsub("%d+",function(m)return m*n end)print(s)' <<< $'This 1 is22a 3352sentence 50\n3'
    This 3 is66a 10056sentence 150
    

    manatwork

    Posted 2014-08-31T04:33:56.993

    Reputation: 17 865

    1You can golf it a bit more by putting everything on one line, and smooshing instructions next to ")". Like "s=r()n=()" is perfectly fine. – AndoDaan – 2014-09-01T15:33:38.953

    1

    Doh, and I read Tips for golfing in Lua just a couple of weeks ago. :( Thank you, @AndoDaan.

    – manatwork – 2014-09-01T16:22:22.227

    2

    JavaScript, ES6, 43 characters

    This is my first attempt at golfing!

    (p=prompt)(p(n=p()).replace(/\d+/g,y=>y*n))
    

    Run this in latest Firefox's Console. The first input is the number and the second input is the string from which the numbers are to be multiplied with the first input number.

    The final prompt lists the output.

    user31482

    Posted 2014-08-31T04:33:56.993

    Reputation:

    Pretty nice JS Golf for a first timer! – Optimizer – 2014-09-29T13:55:11.617

    Many other answers could be shorter if number was read first. That was is much easier. – manatwork – 2014-09-29T13:57:01.007

    @manatwork - Yeah, but the question does not give any preference on the order, so I think it should be fine. – None – 2014-09-29T13:57:54.800

    Correct. This is a lack of the question itself. Personally I preferred to handle input in string+number order in my answers to keep them comparable with the others. – manatwork – 2014-09-29T14:13:28.030

    1

    Perl - 48 chars

    $n=<>;print/\d/?$_*$n:$_ for(<>=~/(\d+)|(\D+)/g)
    

    Read a number on the first line, then read a sentence and break it into chunks of either digits or non digits. Print the non digits as they are, and the numbers get multiplied.

    Tal

    Posted 2014-08-31T04:33:56.993

    Reputation: 1 358

    1

    CJam - 35

    li:X;lN+{_sA,s-,{])\_!!{iX*}*oo}*}/
    

    Try it at http://cjam.aditsu.net/

    Sample input:

    7
    CJ4M W1LL H4V3 R3GUL4R 3XPR35510N5 L4T3R
    

    Sample output:

    CJ28M W7LL H28V21 R21GUL28R 21XPR248570N35 L28T21R
    

    Explanation:

    The program goes through each character, collecting the digits on the stack, and for each non-digit it first prints the collected number (if any) multiplied by the numeric input, then prints the character.

    li:X; reads the numeric input and stores it in X
    lN+ reads the string and appends a newline (it helps with trailing numbers)
    {…}/ for each character in the string
    - _s copies the character and converts to string
    - A,s-, removes all the digits and counts the remaining characters; the result will be 0 if the character was a digit or 1 if not
    - {…}* executes the block if the count was 1 (i.e. non-digit); for digits it does nothing, so they remain on the stack
    -- ] collects the characters from the stack into an array (i.e. a string); the characters are any digits from the previous iterations, plus the current character
    -- )\ separates the last item (the current character) and moves it before the (remaining) string
    -- _!! copies the string and converts it to a boolean value - 0 if empty, 1 if not
    -- {…}* executes the block if the string was not empty, i.e. we had some digits before the current non-digit character
    --- iX* converts the string to integer and multiplies by X
    -- o prints the top of the stack - either the multiplied number or the empty string if we didn't have a number
    -- o (the 2nd one) prints the new top of the stack - the current non-digit character

    aditsu quit because SE is EVIL

    Posted 2014-08-31T04:33:56.993

    Reputation: 22 326

    That's a much saner approach. – Dennis – 2014-09-01T13:17:48.410

    1

    J - 63 char

    Program reads in the number, and then the sentence.

    ;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
    

    Explained by explosion:

    ;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
                                                             1!:1]1  NB. read sentence
                                             e.&'0123456789'         NB. is digit? bool vector
                                          i=.                        NB. save to i
                                     2  /\                           NB. between adjacent chars:
                                      ~:                             NB.  true if not equal
                                   1,                                NB. pad to sentence length
     (                         ;.1~                         )        NB. cut the sentence
                    ^:(0{i)                                          NB. if cut is digits:
      *&.".                                                          NB.  multiply as number
           &(1!:1]1)                                                 NB.  with user input
    ;                                                                NB. rejoin sentence
    

    If we get to use J's PCRE library and make the sentence come first, we can knock this down to 54 characters:

    ;_2(,*&.".&(1!:1]1)&.>)/\'\d+'(rxmatches rxcut])1!:1]1
    

    Explained by explosion:

                                                    1!:1]1  NB. read in sentence
                             '\d+'(rxmatches       )        NB. all /\d+/ matches
                                  (          rxcut])        NB. cut by those matches
     _2                     \                               NB. on each nonmatch-match pair:
       (               &.>)/                                NB.  take the match
         *&.".                                              NB.  multiply as number
              &(1!:1]1)                                     NB.  by user input
       (,                 )                                 NB.  prepend nonmatch
    ;                                                       NB. rejoin sentence
    

    J is bad at this, what can I say. It's a inconvenient because J's so nonimperative.

    Some examples:

       ;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
    3
    This 1 is22a 3352sentence 50
    This 3 is66a 10056sentence 150
       ;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
    100
    N0 R3GUL4R 3XPR35510N5 1N J M4K35 M3 54D ALS0
    N0 R300GUL400R 300XPR3551000N500 100N J M400K3500 M300 5400D ALS0
       0!:0 <'system\main\regex.ijs'  NB. this is usually preloaded by J on startup anyway
       ;_2(,*&.".&(1!:1]1)&.>)/\'\d+'(rxmatches rxcut])1!:1]1
    TH4T'5 M4RG1N411Y B3TT3R
    0
    TH0T'0 M0RG0N0Y B0TT0R
    

    algorithmshark

    Posted 2014-08-31T04:33:56.993

    Reputation: 8 144

    1

    Haskell (161)

    Golfed

    main=do{n<-getLine;l<-getContents;let{g c(t,s)|c>'/'&&c<':'=(c:t,s)|t/=""=([],c:(show.(*(read n)).read$t)++s)|True=(t,c:s)};putStr.tail.snd.foldr g("","")$' ':l}
    

    Ungolfed

    modify :: (Show a, Read a) => (a -> a) -> String -> String
    modify f = show . f . read
    
    main=do
      number <- fmap read $ getLine    -- get number  
      l <- getContents                 -- get input
    
      -- if the current character is a digit, add it to the digits
      -- if the current character isn't a digit, and we have collected
      --    some digits, modify them and add them to the string
      -- otherwise add the current characters to the string
    
      let go current (digits , string) 
            | current `elem` ['0'..'9'] = (current : digits, string)
            | not (null digits)         = ([], current:(modify (*number) digits) ++ string)
            | otherwise                 = (digits, current:string)
    
      -- since the result of `go` is a pair, take the second value,
      -- remove the head (it's a space, in order to convert digits at the start)
      -- and print it
      putStr . tail . snd . foldr go ("","")$' ':l
    

    Unfortunately, Haskell doesn't have a Regex library in its Prelude.

    Zeta

    Posted 2014-08-31T04:33:56.993

    Reputation: 681

    nice golfing ; you could have deleted the outermost {} for a gain of 1 char. also, I just posted this Haskell solution with 70 bytes: http://codegolf.stackexchange.com/questions/37110/multiply-all-numbers-in-a-string/38216#38216

    – proud haskeller – 2014-09-26T20:52:18.480

    1

    flex(-lexer) (94 89 characters)

     int m;main(c,v)char**v;{m=atoi(*++v);yylex();}
    %%
    [0-9]+ {printf("%d",m*atoi(yytext));}
    

    Ungolfed version which doesn't segfault if you forget the command-line argument (not much longer):

    %{
    #define YY_DECL int y(int m)
    %}
    %option noyywrap noinput nounput
    %%
    [0-9]+ {printf("%d",m*atoi(yytext));}
    %%
    int main(int argc, char** argv) {
      return (argc > 1) ? y(atoi(argv[1])) : 1;
    }
    

    Compile with:

    flex -o m.c m.l
    cc -o m m.c -lfl
    

    or:

    flex --noyywrap -o m.c m.l
    cc -o m m.c
    

    Eg:

    $ ./m 163
    This 1 is22a 3352sentence 50
    This 163 is3586a 546376sentence 8150
    

    rici

    Posted 2014-08-31T04:33:56.993

    Reputation: 601

    1

    Groovy - 124

    Scanner s=new Scanner(System.in)
    def x=s.nextLine()
    def n=s.nextInt()
    x=x.replaceAll(/\d+/,{it->it.toInteger()*n})
    println x
    

    Click on the title to see the runnable example

    Examples Tried:

    This 1 is22a 3352sentence 50
    3
    This 3 is66a 10056sentence 150


    This 1 is22a 3352sentence 50
    42
    This 42 is924a 140784sentence 2100

    Little Child

    Posted 2014-08-31T04:33:56.993

    Reputation: 293

    0

    GNU Awk: 86 characters

    s{n=split(s RS,a,/[^0-9]+/,d);while(++i<n)printf"%s%s",a[i]==""?"":a[i]*$1,d[i]}{s=$0}
    

    Sample run:

    bash-4.3$ awk 's{n=split(s RS,a,/[^0-9]+/,d);while(++i<n)printf"%s%s",a[i]==""?"":a[i]*$1,d[i]}{s=$0}' <<< $'This 1 is22a 3352sentence 50\n3'
    This 3 is66a 10056sentence 150
    

    manatwork

    Posted 2014-08-31T04:33:56.993

    Reputation: 17 865

    0

    PHP - 75/115 68/109

    Two versions, newer php versions can do this:

    echo preg_replace_callback("/\d+/",function($m){return$m[0]*$f;},$s);
    

    Older php versions: I did not count the newline, added those for readability.

    function a($s){echo preg_replace_callback("/\d+/",b,$s);}
    function b($i){global$f;return$i[0]*$f;}
    a($s,$f);
    

    Example input+output

    $s = "ab11cd22"; // string
    $f =  3; // -> output: ab36cd69
    $f = -2; // -> output: ab-24cd-46
    

    Kinda difficult, the words 'function' and 'preg_replace_callback' take up a lot of chars.
    The space after global and return arent needed if followed by a $var (-2 chars)

    Martijn

    Posted 2014-08-31T04:33:56.993

    Reputation: 713

    No need to put \d in character class (-2 chars); no need to enclose the function in double quotes (-2 chars); you have to properly terminate the statement inside the function with semicolon (+1 char). By the way, \d in double quoted string should be written as \\d, so better change the quotes to single quotes. – manatwork – 2014-09-01T13:19:06.250

    Thanks. Started with [0-9], changed 0-9 to \d. Wasnt sure about the quotes around the function, I cant test that one, my local php's version doesnt allow it. – Martijn – 2014-09-01T13:23:02.473

    The double quotes work fine (at least for me) :) – Martijn – 2014-09-01T13:26:18.263

    0

    C (142 134 characters)

    main(a,b)char**b;{char*c=b++[2],*d="0123456789";for(;*c;c+=strspn(c
    ,d))write(1,c,a=strcspn(c,d)),dprintf(1,"%d",atoi(*b)*atoi(c+=a));}
    

    Newline inserted for enhanced legibility. Pass the factor as the first, the string as the second command line option. This implementation requires the dprintf function which is part of POSIX.1 2008 and might not be available on Windows. Here is the unminified source:

    /*
     * Multiply all numbers in a string by a number.
     * http://codegolf.stackexchange.com/q/37110/134
     * Solution by user FUZxxl
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    extern int
    main(int count, char **argv)
    {
        char *str = argv++[2], *digits = "0123456789";
    
        while (*str) {          /* as long as we have not reached the end */
            count = strcspn(str, digits);
            write(1, str, count);
    
            dprintf(1, "%d", atoi(*argv) * atoi(str += count));
            str += strspn(str, digits);
        }
    }
    

    Improvements

    • 142 → 134: Use strspn and strcspn instead of looping over the string.

    FUZxxl

    Posted 2014-08-31T04:33:56.993

    Reputation: 9 656

    0

    Clojure - 141 140 128 chars

    I'm a Clojure newbie, but FWIW:

    (let[[a b]*command-line-args*](println ((fn[s n](clojure.string/replace s #"\d+"#(str(*(read-string %)(read-string n)))))a b)))
    

    Sample run:

    bash$ java -jar clojure-1.6.0.jar multstr.clj "This 1 is22a 3352sentence 50" 3  
    This 3 is66a 10056sentence 150
    

    Ungolfed (ugly but hopefully somewhat easier to read):

    (let [[a b] *command-line-args*]
        (println ( (fn [s n] 
                        (clojure.string/replace 
                            s 
                            #"\d+" 
                            #(str (* (read-string %) (read-string n)))))
                        a b)))
    

    Michael Easter

    Posted 2014-08-31T04:33:56.993

    Reputation: 585

    0

    Two answer: +

    Pure bash (~262)

    First, there is an not so short pure bash version (no fork, no external binaries)!

    mn () { 
        if [[ $1 =~ ^(.*[^0-9]|)([0-9]+)([^0-9].*|)$ ]]; then
            local num=${BASH_REMATCH[2]} rhs="${BASH_REMATCH[3]}";
            mn "${BASH_REMATCH[1]}" ${2:-3} l;
            echo -n "$[num*${2:-3}]$rhs";
        else
            echo -n "$1";
        fi;
        [ "$3" ] || echo
    }
    

    Let's show:

    mn "This 1 is22a 3352sentence 50" 42
    This 42 is924a 140784sentence 2100
    

    (Which is a totally improbable sentence)

    Perl little obfuscated (for fun only)

    This version (based on @Chilemagic's answer) is not shorter, but designed as a totem script:

    cat <<eof >mn.pl

    #!/usr/bin/perl          -sp
                          eval eval'"'
                          .('['^'(')
               .'/\\'.'\\'.('`'|'$').'+'.'/\\$\&'
                            .'*\\$'
                          .('`'|'.').'/'
                          .('`'|"'")
                          .('`'|'%').'"'
    

    eof chmod +x mn.pl

    Sample run:

    ./mn.pl -n=2 <<<$'This 1 is22a 3352sentence 50\n21.'
    This 2 is44a 6704sentence 100
    42.
    

    F. Hauri

    Posted 2014-08-31T04:33:56.993

    Reputation: 2 654

    0

    Python 89

    import re,sys
    j,n,s=sys.argv
    print re.sub('(\d+)',lambda m:str(int(m.group())*int(n)),s)
    

    eg:

    # python numberstring.py 3 '1shoop da2 w007p!'
    3shoop da6 w21p!
    

    Sammitch

    Posted 2014-08-31T04:33:56.993

    Reputation: 509

    0

    Rebol - 117

    n: do input d: charset"0123456789"parse s: input[any[b: some d e:(e: insert b n * do take/part b e):e | skip]]print s
    


    Ungolfed:

    n: do input 
    d: charset "0123456789"
    
    parse s: input [
        any [
            b: some d e: (e: insert b n * do take/part b e) :e
            | skip
        ]
    ]
    
    print s
    

    draegtun

    Posted 2014-08-31T04:33:56.993

    Reputation: 1 592

    0

    Python - 142

    import re
    s=raw_input()
    i=int(raw_input())
    print ''.join([x[0]+str(int(x[1])*i) for x in filter(None,re.findall('(\D*)(\d*)',s)) if x[0]!=''])
    

    Xirion11

    Posted 2014-08-31T04:33:56.993

    Reputation: 1

    0

    Java 218

    Someone had to do java. The input string are 2 token on the command line.

    java M 'This 1 is22a 3352sentence 50' 3

    public class M{
        public static void main(String[]a) {
            int m=new Integer(a[1]),n=0,b=0;
            String s="";
            for(char c:a[0].toCharArray()){
                if(c<48|c>57)s=b>(b=0)?s+m*n+c:s+c;
                else n=b*n+c-38-(b=10);
            }
            System.out.println(b>0?s+m*n:s);
        }
    }
    

    Florian F

    Posted 2014-08-31T04:33:56.993

    Reputation: 591

    1According to my test, the if condition works with bitwise | too, which is 1 character shorter. – manatwork – 2014-09-06T12:19:35.000

    Thanks. Code updated. (It is actually still a boolean or, just without evaluation shortcut). – Florian F – 2014-09-06T13:13:38.123

    0

    Haskell, 70

    too bad I'm too late IMHO this is pretty good for this specific question and language. the other Haskell solution here is 161 characters..

    []%n=""
    s%n=f s(reads s)n
    f _((a,b):_)n=show(a*n)++b%n
    f(x:s)_ n=x:s%n
    

    this works by using the reads function which parses a string Partially. for example, reads "34abc" = [(34, "abc")]. this obviously makes it perfect for this challenge.

    usage:

    *Main> " This 1 is22a 3352sentence 50"%3
    " This3 is66a10056sentence150"
    

    proud haskeller

    Posted 2014-08-31T04:33:56.993

    Reputation: 5 866