Sunny
Sunny

Reputation: 119

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/

Upvotes: 2

Views: 87

Answers (2)

The fourth bird
The fourth bird

Reputation: 163632

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

Upvotes: 1

bobble bubble
bobble bubble

Reputation: 18555

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

Upvotes: 2

Related Questions