简体   繁体   中英

RegEx for matching any char except only 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/

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM