0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Get number from string in python

Last updated at Posted at 2022-11-01
import sys
import re

a = sys.argv[1]
print(a)
m1 = re.search("[^0-9\.]", a) # str
m2 = re.search("[0-9\.]", a)  # num
if m1 and m2:
  print("m1.start(),m2.start():",m1.start(),m2.start())
  if m1.start() > m2.start():
    ret = a[:m1.start()]
  else:
    ret = a[m2.start():]
    print("ret:",ret)
    m3 = re.search("[^0-9\.]", ret)
    if m3:
      ret = ret[:m3.start()]
elif m1:
  # only str
  ret = ""
elif m2:
  # only num
  ret = a
print("ret:",ret)
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?