Can regular expressions be used to perform arithmetic? Such as find all numbers in a file and multiply them by a scalar value.
Can regular expressions be used to perform arithmetic? Such as find all numbers in
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I prepared a small script which uses
re.finditerto find all the integers (you can change the regexp so that it can deal with floats or scientific notation) and then usemapto return a list of scaled numbers.If you have a
filewhose numbers you want to scale by a factorfact, you call the script from the command line aspython script.py file factand it will print toSTDOUTall the scaled numbers. Of course, you can do something more useful if you wanted…