drm30963 2017-05-01 20:49
浏览 231

正则表达式允许一个或多个单词允许一个空格并使用两个或更多空格作为列

i am trying to parse some files line by line and trying to identify it as columns. Two columns that are consecutive are words, but the separation pattern is more than one space. As the columns can have spaces between, i am having some trouble separating these two.

Examples of lines:

2236        ARGEMIRO PATROCINIO                                   ARGEMIRO                 I       I          UBC            3,8462

1150721     ZACHARY F CONDON                                      ZACH CONDON               I       I          FINTAGE        8,3333

50300       COMERCIAL FONOGRAFICA RGE LTDA.                                                 PF      LI         ABRAMUS       25,0000`

(fixed)

obs.: it's not showing all the spaces between '2236', 'ARGEMIRO PATROCINIO', 'ARGEMIRO', 'I', 'I', 'UBC' and '3,8462'

I am using this regex:

(\d+)\s+([\.a-zA-Z\s,'À-úÀ-ÿ()\?\-\/\d]+)\s{2,}([\.a-zA-Z\s,'À-úÀ-ÿ()\?\-\/\d]+)\s{2,}(I|PF|MA)\s{2,}(I|PF|PL|LI|MA|CV|MJ)\s{2,}(\w+)\s{2,}(\d+,\d{4})

but unfortunately, "ARGEMIRO PATROCINIO" is coming with the second "ARGEMIRO"; "ZACHARY F CONDON" with the second "ZACH CONDON" and on.

So,

  1. how can i fix this regex to separate these two "columns"?
  2. how would be another regex that can grab anything between two or more spaces within these 7 columns?

Thank you!

  • 写回答

3条回答 默认 最新

  • doumicheng6732 2017-05-01 20:58
    关注

    I'm not actually seeing double spaces in the data you pasted, but you are describing it as such. You can do this to split anywhere there is 2 or more sequential spaces:

    preg_split("/[\s]{2,}/", $data);
    

    DEMO: http://www.phpliveregex.com/p/jWZ (click "preg_split" on the right)

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染