Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用正则表达式截断尾部组_Python_Regex - Fatal编程技术网

Python 用正则表达式截断尾部组

Python 用正则表达式截断尾部组,python,regex,Python,Regex,我有两个字符串,它们都以一些数字结尾,前面紧跟着一些非数字字符,例如,10.1002/0470868279-ch1。我想完全切断这一组;对于本例-ch1。我试过了 import re str0 = '10.1002/0470868279-ch1' a = re.match('(.*)([^0-9]+[0-9]*)', str0) print(a.group(0)) print(a.group(1)) print(a.group(2)) str1 = '10.1002/0470868279.

我有两个字符串,它们都以一些数字结尾,前面紧跟着一些非数字字符,例如,
10.1002/0470868279-ch1
。我想完全切断这一组;对于本例
-ch1
。我试过了

import re

str0 = '10.1002/0470868279-ch1'

a = re.match('(.*)([^0-9]+[0-9]*)', str0)
print(a.group(0))
print(a.group(1))
print(a.group(2))

str1 = '10.1002/0470868279.1'  # likewise
但事实并非如此:

10.1002/0470868279.ch1
10.1002/0470868279.c
h1
我想正则表达式从一开始就非常匹配


有什么提示吗?

添加
使第一个匹配成为非贪婪匹配,使其尽可能少地匹配。还要添加
$
,以便匹配项始终位于字符串末尾:

a = re.match('(.*?)([^0-9]+[0-9]*)$', str0)

以下内容应该匹配到您想要的结尾:
(-124\+)?\ d+(\.\d*)?/(-124\+)?\ d+(\.\d*)?
这将允许您提取您要查找的数字

此模式将仅匹配末端部分(在
-ch1
示例上测试):
(?使用