[英]How can I sort a list for sentence alphabetically in python based on 2nd or 3rd word or something like that
[英]Python Regex match 2nd or 3rd word in line
我正在嘗試使用正則表達式將線條分成 3 個部分,典型的線條適合這種模式: -30.345 150.930 112.356
我正在使用lat = float(re.match('[^\\s]+', line).group(0))
提取第一部分數據,但無法正確定位第二個和第三個數字。
我試過/正在嘗試long = float(re.match('.*?\\s(\\S+)\\s.*?', line).group(0))
但這會返回整個字符串,直到第二個空格。 如何僅針對這些字符串中的第二個數字和第三個數字?
如果你不能做split
那么你可以在開始時用可選的-
或+
匹配數字:
>>> s = '-30.345 foo 150.930 abc 112.356 another .123'
>>> re.findall(r'([+-]?\d*\.?\d+)', s)
['-30.345', '150.930', '112.356', '.123']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.