Squiggle

Registered
Aug 13, 2013
3
0
1
cPanel Access Level
Website Owner
Hello all, first post so please be gentle :)

(also not a programmer so just trying to find my way in the regex syntax - looked at various sites but can't crack the problem as yet) ...


I have an account filter that I am trying to set up that will catch a four letter string ... unfortunately the letters can be spaced in various ways:

A B C D
AB C D
A-B_C D

etc etc

I have been using A[ -_]B[ -_]C[ -_]D which catches everything except where two characters are together (like second example above).

Is there an easy way to include "no-space" as a character so that the second example above would also be caught?


Thanks in advance (and sorry if this is really simple!)
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,913
2,201
363
Hello :)

I have moved this over to the "cPanel Developers" forum because you may get more user input related to regex syntax here.

Thank you.
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
? will match 0 or 1 instance of a pattern so you'll want to use that after your groupings for space, -, _, like so:

A[ -_]?B[ -_]?C[ -_]?D

If that is the entire string you're trying to match, you can use the ^ (starts with) and $ (ends with) operators as well so that you don't match something like rstlneA B-CDxyz:

^A[ -_]?B[ -_]?C[ -_]?D$
 

Squiggle

Registered
Aug 13, 2013
3
0
1
cPanel Access Level
Website Owner
Just a quickie Dave,

I have given this a go and the first example works great but the second doesnt seem to capture anything:

If I use (say) M[ -_]?O[ -_]?N[ -_]?K it captures all sorts of MONK, M-on k, m o-n-k which is great however, as you noted, it also catches "monkey" etc

When I tried the second example ^M[ -_]?O[ -_]?N[ -_]?K$ it doesnt seem to catch anything.

Am I missing something in how I am using the second example ... should this catch where there is a space at either end (" monk ")?

Sorry if this is basic syntax but I am a complete newbie at this :)
 

AndrewH.

Well-Known Member
Dec 10, 2012
52
3
83
cPanel Access Level
Root Administrator
The '$' is a special character that indicates the end of a regex, so the second regex '^M[ -_]?O[ -_]?N[ -_]?K$' is basically doing this...

(^) Find a string that starts with the following match
(M) Match the character 'M'
([ -_]) Match any of the character set ' ', '-', or '_'
(?) Match the previous step 0 or 1 times
(O) Match the character 'O'
([ -_]) Match any of the character set ' ', '-', or '_'
(?) Match the previous step 0 or 1 times
(N) Match the character 'N'
([ -_]) Match any of the character set ' ', '-', or '_'
(?) Match the previous step 0 or 1 times
(K) Match the character 'K'
($) The previous character must be the end of the string


If you remove the ^ and $ it will find any instance of MONK, including " MONK", " MONK ", "MONK ", "ASDFMONKASDF". If you want spaces you can add [ ]* to the beginning or end of the regex like so: ^[ ]*M[ -_]?O[ -_]?N[ -_]?K[ ]*$
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
When I tried the second example ^M[ -_]?O[ -_]?N[ -_]?K$ it doesnt seem to catch anything.
If you use ^, it means, this will appear at the beginning of my string. So if the string is 'aMONK', it won't match. It will need to be 'MONK', or 'M-O NK', or anything else that starts with a M.

Am I missing something in how I am using the second example ... should this catch where there is a space at either end (" monk ")?

Sorry if this is basic syntax but I am a complete newbie at this :)
Nope, it should not catch anything with a space at either end. This specifically asks for a M at the beginning and K at the end. If you also want to catch spaces, you need to add them like so (also accounting for lowercase):

[ ]?[Mm][ -_]?[Oo][ -_]?[Nn][ -_]?[Kk][ ]?

Think of each [ ] in the above as a character. [Nn] means the next character will be either 'N' or 'n'. [ -_] means the next character will be either ' ', '-', '_', or if none of those, ignore these choices and test the next pattern found. [ ]? means it will either be ' ', or if not a ' ', ignore this and test the next pattern found.

If the line always starts with space or M/m, add ^ to the beginning of this. Match only to the character K/k or ' ' by adding $ to the end of it.