0

I have a string in the following form :

"425x344"

Now I'd like to resolve first and second numbers from this string as two separate variables. How can I do this ? I've created this regex:

regex = re.compile(r"^(\d+x\d+)")

to check if the string form is proper. But what next ?

2
  • Use ...$ and .match (or ^...$ and .search) to make sure the whole sting is consumed. As of now, both ways of running the regex accept 1x1foo. Commented Nov 24, 2010 at 15:19
  • And using $ will accept 1x1\n Commented Nov 24, 2010 at 21:07

5 Answers 5

5

a, b = '425x344'.split('x')

Sign up to request clarification or add additional context in comments.

9 Comments

Although that will give you the numbers as strings.
The OP just said he/she wants it in two separate variables.
a, b = [int(n) for n in '425x344'.split('x')]
@Rafael, int, str and the other builtin class constructors are one of the few cases where map is better than a list comprehension. The more you know.
@aaronasterling nice to know, but what's the rationale?
|
2

Since you're filtering it with a regex, you can just do

a, b = map(int, s.split('x'))
res = a * b

If you're planning on multiplying, it can be done in one line:

res = eval(s.replace('x', '*'))

or

res = (lambda x, y: x * y)(*map(int, s.split('x')))

With an import and one line, this can be done with

import operator

res = operator.mul(*map(int, s.split('x')))

You'll have to profile them to see which is faster.

2 Comments

Nevermind. It's because I was missing a Parenthesis. Being a Chimpanzee, I get discriminated against so much that I've gotten a little sensitive. Thanks to the anonymous downvoter for the helpful error message. You should get a job as a C compiler.
... or a Python compiler ... Syntax Error: invalid syntax
1

Change it to:

regex = re.compile(r"^(\d+)x(\d+)")

Then use regex.match(my_string) to get the MatchObject out, and you can use match.group(1) and match.group(2) to get the variables out.

1 Comment

group(0) returns the full match text. You want group(1) and group(2).
0

You probably mean "values", or "literals". 425 is not typically a valid name for a variable, which tend to have symbolic names.

If you change your regular expression to capture the numbers separately:

regex = re.compile(r"^(\d+)x(\d+)")

you can then use code like this:

str = "425x344"
mo = regex.search(str)
if mo != None:
  print "%s=%d" % (str, int(mo.group(1)) * int(mo.group(2)))

to compute the result.

Comments

0

If you are wanting to do it with re, http://effbot.org/zone/xml-scanner.htm might be worth a read. It shows how to properly split each argument in a expr with re

import re

expr = "b = 2 + a*10"

for item in re.findall("\s*(?:(\d+)|(\w+)|(.))", expr):
    print item

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.