St3an
St3an

Reputation: 806

expression not matching regex

grep (GNU grep) 2.16

given the following string: It's pretty late but I wanna regexp_ _this !

grep '[a-zA-Z]+' hits everything but ', _ and ! characters (ok)

but grep '[a-zA-Z]+[a-zA-Z0-9_]*' hits nothing (in my example regexp_ should match, but not _this (nok)

to sum up, I need to forbid words beginning with something else than a letter, and then allow alphanum & '_' character (0 or more time(s))

I've tried on https://regex101.com, both regexp work as expexted. Only in my bash shell (as well as in php code I'm writing), the second regexp hits nothing.

EDIT : @anubhava #1 solution works fine. I nevertheless want to do it via php code : preg_match('/[a-zA-Z]\+[a-zA-Z0-9_]*/',$myString) doesn't work...

RE-EDIT : I misunderstood the use of PHP preg_match, sorry

Upvotes: 2

Views: 119

Answers (1)

Tim Malich
Tim Malich

Reputation: 1391

I've create a dummy-file with the content:

regexp_
asdf
_this
R_

the bash command (I've added the ^):

grep '^[a-zA-Z]\+[a-zA-Z0-9_]' dummy

gave me the result:

regexp_
asdf
R_

In PHP the regex needs to look like this (I've added the ^ and removed the \ before the +):

preg_match('/^[a-zA-Z]+[a-zA-Z0-9_]*/',$myString);

I hope that's what you needed.

Upvotes: 1

Related Questions