Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this regexp mean? [duplicate]

Tags:

regex

So, what does next regexp actually mean?

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

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

like image 580
Denys S. Avatar asked Feb 20 '26 00:02

Denys S.


2 Answers

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..

like image 159
stema Avatar answered Feb 21 '26 13:02

stema


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.

like image 40
ohaal Avatar answered Feb 21 '26 13:02

ohaal