python - 需要正则表达式 :(

标签 python regex

我正在使用 Python,我需要能够获取以下形式的字符串

abc | pqr | [1,2,3,4,5]

并从中获取实际的整数数组 [1,2,3,4,5]。有什么建议吗?

最佳答案

假设abc | pqr | 部分是文字字符,您想要:

import re
import ast

m = re.match(r"abc \| pqr \| (\[[-0-9,]*\])", inString)
if m is not None:
    theList = ast.literal_eval(m.group(1))

如果您想跳过前导的不匹配字符,请使用search 而不是match

关于python - 需要正则表达式 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4103544/

相关文章:

python - Django Lightsail 上没有应用程序文件夹

python - 发出 GET 请求,然后用 Python (Twisted) 打印出响应体

regex - 使用正则表达式从 Google BigQuery 中的字符串中提取数字

r - 将字符向量拆分成句子

python - BeautifulSoup4 Python 3.4 抓取问题

python - 带参数调用程序

python - 在 Python 中验证字符串值

javascript - 在 javascript : leaving a word in between 中使用正则表达式进行比较和替换

python - django haystack 在生产中,如何自动执行 python manage.py update_index ?

regex - 正则表达式只允许 1-20 范围内的数字