python - 如何使用 Python 正则表达式在多行文本中查找重复模式?
问题描述
我对 Python 的正则表达式模块非常陌生。我正在尝试查找提出问题的问题编号和相应的公司名称。我的文字如下所示:
文字输入:
text = """
# Daily Coding Problem
Solutions to problems sent by dailycodingproblem.com
---
#### Problem 1
Given a list of numbers, return whether any two sums to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
[Solution](solutions/problem_001.py)
---
#### Problem 2
This problem was asked by Uber.
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
Follow-up: what if you can't use division?
[Solution](solutions/problem_002.py)
---
#### Problem 3
This problem was asked by Google.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
[Solution](solutions/problem_003.py)
---
"""
import re
from pathlib import Path
pat = r"Problem (\d+)$\n.*asked by (.*)\.$"
out = re.findall(pat,text,flags=re.MULTILINE)
print(out)
"""
我的代码尝试:
import re
pat = r"^Problem (\d+).* asked by (\w+[\s]\w+)."
out = re.findall(pat, text, flags=re.MULTILINE|re.DOTALL)
print(out)
# [('1', 'Google company')]
但我得到了错误的输出。如何获得正确的预期答案:
problem_num = [2,3]
company = ["Uber", "Google"]
解决方案
我假设带有“询问者”的行总是在问题编号之后。对我来说,它适用于模式。
pat = r"### Problem (\d+)$\n*.*asked by ([a-zA-Z]+)\."
out = re.findall(pat,text,flags=re.MULTILINE)
$ - 由于 MULTILINE 标志而导致的行尾
请注意,这将获得“谷歌公司”而不是“谷歌”
推荐阅读
- python - 基于另一个 DataFrame 中的两列的子集 pandas DataFrame
- python - 如何在字典中使用元组来获取 psycopg2 中的命名参数
- node.js - 替换逗号nodejs之间的空值
- apostrophe-cms - 条件字段:根据其他字段的值隐藏/显示字段
- html - 桌面上的一个高 div 紧挨着两个较短的 div,并使用 Bootstrap 4 在移动设备上堆叠
- python - 泰坦尼克号数据集。不同形状误差的Logits和Label
- angular - 仅在 iOS 移动 Safari 中上传图像时出现错误的 CORS 失败
- java - 比较 RGB 值
- google-apps-script - 如何向谷歌应用脚本网络应用的“/dev”URL 进行身份验证
- javascript - 使用正则表达式引用 javascript 对象键?