Denys S.
Denys S.

Reputation: 6525

What does this regexp mean?

So, what does next regexp actually mean?

str_([^_]*)(_[_0-9a-z]*)?

And is it equal to ^str_[^_]*(_[_0-9a-z]*)??

Upvotes: 2

Views: 358

Answers (3)

stema
stema

Reputation: 93046

str_ matches these characters literally

([^_]*) matches any character 0 or more times that is not a underscore

(_[_0-9a-z]*)? matches another underscore and then 0 or more of the characters _0-9a-z. This last part is optional.

I wrote recently a really brief regex introduction, hope it will help you..

Upvotes: 5

ohaal
ohaal

Reputation: 5268

As mentioned in the comments to my answer, http://gskinner.com/RegExr/ explains everything about a regular expression if you put it in.

str_([^_]*)(_[_0-9a-z]*)?
\  /^\  /^^^^\       /^^^
 \/ | \/ |||| \     / |||
 |  | |  ||||  \   /  ||`- Previous group is optional
 |  | |  ||||   \ /   |`-- End second capture group (an underscore and any amount of characters _, 0-9 or a-z)
 |  | |  ||||    |    `--- Match any amount (0-infinite) of previous (any character _, 0-9 or a-z)
 |  | |  ||||    `-------- Match any of the characters inside brackets
 |  | |  ||||              (0-9 means any number between 0 and 9, a-z means any lower case char between a and z)
 |  | |  |||`------------- Match "_" literally
 |  | |  ||`-------------- Start second capture group
 |  | |  |`--------------- End first capture group (any amount of any character which is not underscore)
 |  | |  `---------------- Match any amount (0-infinite) of previous (any character which is not underscore)
 |  | `------------------- ^ at the start inside [] means match any character not after ^
 |  |                      (In this case, match any which is not underscore)
 |  `--------------------- Start first capture group
 `------------------------ Match "str_" literally

The ^ at the start of ^str_[^_]*(_[_0-9a-z]*)? just means that it should only match at the start of line of anything you input.

Upvotes: 4

gaussblurinc
gaussblurinc

Reputation: 3692

str_([^_]*)(_[_0-9a-z]*)?

so, let's talk on regexp:

1) ([^_]*) - catch () in any count(zero is possible) * symbols [], that are not ^ this symbol _.

2) (_[_0-9a-z]*)? - to be or not to be ? and catch () sequence with beginning symbol _ and tail of sequence in which appear in any count * any symbol from set [] like _ a,b,c,..,z or 0,1,..9

Upvotes: 0

Related Questions