GPGVM
GPGVM

Reputation: 5619

Regex pattern misses match on a 2 char word

Using regex101 I have developed this regex:

^(\S+)\s_(\S)(\S[^;\s]+)?.*

This works great for 99.999% of the time but occasionally it is run against a string containing a 2 char word that should have matched.

For example it would normally capture...

string _asdf = string.empty;
bool _ttfnow;
//$1 = string
//$2 = a
//$3 = sdf
and
//$1 = bool
//$2 = t
//$3 = tfnow

But for some reason this fails to match the third group?

string _qw = string.empty;
//$1 = string
//$2 = q
//$3 = 

Again using regex101 if add add a char it suddenly matches so:

string _qwx = string.empty;
//$1 = string
//$2 = q
//$3 = wx

Any ideas? Thank You

Upvotes: 1

Views: 52

Answers (2)

karthik manchala
karthik manchala

Reputation: 13640

[^;\s]+ change it to [^;\s]*

/^(\S+)\s_(\S)(\S[^;\s]*)?.*/

Upvotes: 1

vks
vks

Reputation: 67968

^(\S+)\s_(\S)(\S[^;\s]*)?.*

                     ^^

Just change the quantifier.See demo.

https://regex101.com/r/pG1kU1/33

Upvotes: 1

Related Questions