首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中获取":“之后的文本的Regex

在Python中获取":“之后的文本的Regex
EN

Stack Overflow用户
提问于 2020-12-03 21:59:42
回答 4查看 99关注 0票数 2

我一直在尝试不同的组合来提取":“之后的文本。

代码语言:javascript
复制
    materials[3] = 'PE HD Monofilament Yarn CFR India Assessment Main Ports Spot 2-4 Weeks Full Market Range Weekly (Low) : USD/tonne'

    re.match(r'(?<=:+.)(.*)', materials[3])

但是我在PyCharm上尝试了不同的错误,尽管在https://regexr.com/测试和模拟阅读时,序列aobe是可以的。

从Python检索的错误如下:

代码语言:javascript
复制
  re.match(r'(?<=:+.)(.*)', materials[3])
Traceback (most recent call last):
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-210-556fd124536f>", line 1, in <module>
    re.match(r'(?<=:+.)(.*)', materials[3])
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\re.py", line 173, in match
    return _compile(pattern, flags).match(string)
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\re.py", line 286, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\sre_compile.py", line 768, in compile
    code = _code(p, flags)
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\sre_compile.py", line 607, in _code
    _compile(code, p.data, flags)
  File "C:\Users\p119124\AppData\Local\Programs\Python\Python37\lib\sre_compile.py", line 182, in _compile
    raise error("look-behind requires fixed-width pattern")
re.error: look-behind requires fixed-width pattern 

我可以请您帮个忙吗?

这个想法只是为了提取“美元/吨”。

EN

回答 4

Stack Overflow用户

发布于 2020-12-03 22:19:08

re中的查找模式必须匹配一个固定长度的字符串。

使用捕获组:

代码语言:javascript
复制
import re
materials = 'PE HD Monofilament Yarn CFR India Assessment Main Ports Spot 2-4 Weeks Full Market Range Weekly (Low) : USD/tonne'
match = re.search(r'.*:\s*(.+)', materials)
if match:
  print(match.group(1))

Python证明和a 正则证明

表达式解释

代码语言:javascript
复制
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  :                        ':'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
票数 3
EN

Stack Overflow用户

发布于 2020-12-03 22:23:30

根本没有理由使用正则表达式。对于这样一个简单的用例,str.split将很好地工作。

代码语言:javascript
复制
materials[3].split(':')[1].strip()
票数 3
EN

Stack Overflow用户

发布于 2020-12-03 22:14:11

试试这个:

代码语言:javascript
复制
print(re.search(r'(\s+?:\s+)(.*)', materials).group(2))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65134782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档