0

I am currently using a regex to match a certain line such as:

(.0025 = 625 dollars per contract)

I have a regex which I used for a similar expression, that gets the job done. But, I wanted to know if there was a better implementation for it:

new_string = re.search(r'.*?\([^$]*?([\$|\d][^)]* per contract)\)', cell)

I know I can change point where it looks for a dollar sign, but any other things?

If you could also explain your regex, it would be helpful.

1
  • 4
    If you want some (useful) help, you must explain with precision what you are looking for. example: is the word "dollars" always present, what is your final goal, what do you want to extract exactly. Commented Jul 25, 2013 at 2:40

1 Answer 1

1

If you always want to gather the price, you can do this:

regex_string = r'= (.*? per contract)'

This catches:

'625 dollars per contract'

All it does is take anything between the equal sign (and space after it) and the words "per contract." Since your regex assumes that the words "per contract" will always be present, so does mine.

If you just want the price, you can do this instead:

>>>regex_string = r'= (.*?) per contract'
>>>matchobj = re.search(regex_string, "(.0025 = 625 dollars per contract)")
>>>matchobj.groups()
('625 dollars',)
Sign up to request clarification or add additional context in comments.

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.