0

I have a string thath goes like that:

<123, 321>

the range of the numbers can be between 0 to 999.

I need to insert those coordinates as fast as possible into two variables, so i thought about regex. I've already splitted the string to two parts and now i need to isolate the integer from all the other characters. I've tried this pattern:

 ^-?[0-9]+$ 

but the output is:

[]

any help? :)

2
  • Maybe just take two substrings: one from 1 to the position of the , and the other from the position of the comma + 2 to the end of the string - 1. Commented May 12, 2015 at 6:04
  • regex is probably slower than you think Commented May 12, 2015 at 6:23

3 Answers 3

2

If your strings follow the same format <123, 321> then this should be a little bit faster than regex approach

def str_translate(s):
    return s.translate(None, " <>").split(',')

In [52]: str_translate("<123, 321>")
Out[52]: ['123', '321']
Sign up to request clarification or add additional context in comments.

1 Comment

If the <> are always in the same place, s[1: -1] will be slightly quicker than the translate. You can split on ', ' if you need to get rid of the space.
1

All you need to do is to get rid of the anchors( ^ and $)

>>> import re
>>> string = "<123, 321>"
>>> re.findall(r"-?[0-9]+", string)
['123', '321']
>>> 

Note ^ $ Anchors at the start and end of patterns -?[0-9]+ ensures that the string consists only of digits.

That is the regex engine attempts to match the pattern from the start of the string, ^ using -?[0-9]+ till the end of the string $. But it fails because < cannot be matched by -?[0-9]+

Where as the re.findall will find all substrings that match the pattern -?[0-9]+, that is the digits.

4 Comments

"the string consists only of strings"? your answer is correct but that part was confusing even for a regex veteran
@oxymor0n Sorry I didn't get you
Your note is just... confusing, that's all. Removing the anchors is correct, but your note on why that is doesn't really explain the problem.
That was a typo i actually meant ensures that the string consists only of digits. Is it still unclear, I will edit if so. please do mention
0

"^-?[0-9]+$" will only match a string that contains a number and nothing else.

You want to match a group and the extract that group:

>>> pattern = re.compile("(-?[0-9]+)")
>>> pattern.findall("<123, 321>")
['123', '321']

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.