drlndkhib08556095
2019-05-13 19:48 阅读 90
已采纳

RegEx用于匹配任何char,只有white-space

I am writing a php class to extract data from csv file. So I need help in regular expression.

data sample

Data 
Data        
Datatest1
Data test
Data         867$33@!.//()7
Field somthing
Field           

Regular Expression

/(?:Data|Field)(.+)/

This should not match line 1,2 and 7 because it have only space and tab (whitespace) after Data and Field

here is my regex tester link https://regex101.com/r/xpG25l/1/

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doushe2513 doushe2513 2019-05-13 19:52

    You can do something like

    (?:Data|Field)\h*\S.*
    

    to require an \S (non white-space character) after any amount of \h (horizontal space).

    See your updated demo

    点赞 评论 复制链接分享
  • drd2551 drd2551 2019-05-13 19:52

    You could use a negative lookahead (?!\h*$) to assert what is on the right is not 0+ times a horizontal whitespace character \h* followed by the end of the string $

    (?:Data|Field)(?!\h*$).+$
    

    Regex demo

    If you regex should start matching from the start of the string you can append ^ to the pattern to assert the start of the string.

    Or else in the string test Field somthing there will be a match for Field somthing

    点赞 评论 复制链接分享

相关推荐