【问题标题】:How to write regex for the following experssions to split the data [closed]如何为以下表达式编写正则表达式来拆分数据[关闭]
【发布时间】:2019-09-23 06:42:19
【问题描述】:

1 - Supr Senior:M66tzer,比尔) first_name= Bilr '(' as first name = Roger and last name= Dell..for any string it shuld read until ( and split the name as first name and last name how to write regex

【问题讨论】:

  • 欢迎来到 SO!不幸的是,这是off topic——有很多方法可以做到这一点。您可以发布minimal reproducible example 来展示您尝试过的内容以及遇到问题的地方吗?谢谢。
  • 你能帮我拆分吗
  • 你颠倒了 Joseph Bannet 的 f_namel_name
  • 你能告诉更新的问题如何使用正则表达式
  • 你似乎破坏了你的问题。

标签: python regex split


【解决方案1】:

应该支持以上所有情况

下面的代码可以。它不使用split(),而是使用search()捕获括号

for case in ''' Pocatello, ID/ (Habashi, Yashar)
 Idaho Falls, ID/ (Pigott, Joseph)
 orian flex joy, Lex/ (Bannet, Joseph)'''.split('\n'):
    l_name, f_name = re.search(r'\((.*), (.*)\)', case).group(1, 2)

【讨论】:

  • 这很好,我现在需要加入全名 Yashar habashi,joseph pigott,joseph bannet 如何加入全名
  • 当我尝试时,我得到了 '(Bannet, Joseph)' 这个 '( 除了我需要全名 Bannet Joseph
  • 一种加入方式是fullname = ' '.join((f_name, l_name))
  • 你确定不是l_name Bannet, f_name Josephm_name 亚历克斯?
  • 我已经更新了我的问题,你能告诉正则表达式那个条件
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多