0

I have a string for example:

str1 = "'107.8X98x3.75'"
[107.8, 98, 3.75]
str2 = 'L=130.5mm;L=90mm'
[130.5, 90]
str3 = '圓278.5x15t 304'
[278.5, 15, 304]

I know how to extract by int and float, but I don't want to miss which the numbers appear.

1
  • I am not sure why this is downgraded while this title is visited more than 2k. Commented Feb 16, 2021 at 7:51

1 Answer 1

11

How about regular expressions?

Example

>>> import re
>>> str1="107.8X98x3.75"
>>> re.findall(r'\d+(?:\.\d+)?', str1)
['107.8', '98', '3.75']
Sign up to request clarification or add additional context in comments.

1 Comment

I'am adding some explanation for regex: r for raw expression (I am not sure this is necessary), \d+ some digits whether followed by (<regex2>) or not due to ( ) '?' (0 or 1 option). <regex2> is '?:' uncapture this group enclosed by ( ), '\.' is literal dot rather than use dot as metacharacter, followed by \d+ digits then it becomes ".123".

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.