【问题标题】:Find regular expression in text using Python 3.8使用 Python 3.8 在文本中查找正则表达式
【发布时间】:2019-12-10 15:53:09
【问题描述】:

我正在尝试使用正则表达式从一些文本中提取钻孔编号列表(现在只是一小部分)但不能完全让 regex 按预期工作。

格式通常是两个大写字母后跟多个不同长度的数字,例如BH1, WS01, BH001

偶尔以大写字母结尾,表示它已被重新钻孔,例如BH2301A 是 BH2301 的重演。

我能得到的最接近的是返回一个 tuple 但我只需要返回第一个值。

BH = re.compile(r'\w\w\d+')
BH.findall('4.9.26.9.2  A summary of rotary coring data from White Chalk (South) is provided in Table 37. Downhole geophysics (optical and acoustic televiewers) was undertaken on five boreholes in the LTC Pumping Tests investigation BH2313A, OH03001, OH03002, OH03003, OH04007 and OH04008. Televiewer data provides a more accurate assessment of fracture location and orientation than the associated core logging. This is because the borehole wall is less susceptible to drilling induced disturbance than the recovered core. An automated discontinuity picking algorithm within WellCAD© software was used by the contractor to identify features in the televiewer datasets. ')
['BH2313', 'OH03001', 'OH03002', 'OH03003', 'OH04007', 'OH04008']

BH = re.compile(r'(\w\w\d+(\w)?)')
BH.findall('4.9.26.9.2  A summary of rotary coring data from White Chalk (South) is provided in Table 37. Downhole geophysics (optical and acoustic televiewers) was undertaken on five boreholes in the LTC Pumping Tests investigation BH2313A, OH03001, OH03002, OH03003, OH04007 and OH04008. Televiewer data provides a more accurate assessment of fracture location and orientation than the associated core logging. This is because the borehole wall is less susceptible to drilling induced disturbance than the recovered core. An automated discontinuity picking algorithm within WellCAD© software was used by the contractor to identify features in the televiewer datasets. ')
[('BH2313A', 'A'), ('OH03001', ''), ('OH03002', ''), ('OH03003', ''), ('OH04007', ''), ('OH04008', '')]

元组输出是我的正则表达式所能期望的最好的吗?我需要一个循环来分割这些以返回第一组吗?

【问题讨论】:

  • 我喜欢使用 regex101.com 进行正则表达式故障排除
  • 使用search 而不是findall
  • 仅供参考:\w 包括数字。

标签: python regex findall


【解决方案1】:

刚刚从你得到的元组中提取了第一个值:

[x[0] for x in BH.findall('4.9.26.9.2  A summary of rotary coring data from White Chalk (South) is provided in Table 37. Downhole geophysics (optical and acoustic televiewers) was undertaken on five boreholes in the LTC Pumping Tests investigation BH2313A, OH03001, OH03002, OH03003, OH04007 and OH04008. Televiewer data provides a more accurate assessment of fracture location and orientation than the associated core logging. This is because the borehole wall is less susceptible to drilling induced disturbance than the recovered core. An automated discontinuity picking algorithm within WellCAD© software was used by the contractor to identify features in the televiewer datasets. ')]

【讨论】:

  • 感谢您的快速回复!我从 BH.findall (文本)中创建了一个名为 ID 的新变量,并 [i[0] for i in ID]
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多