The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

CPanel and Regex

Discussion in 'cPanel Developers' started by Squiggle, Aug 13, 2013.

  1. Squiggle

    Squiggle Registered

    Joined:
    Aug 13, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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!)
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,811
    Likes Received:
    671
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    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.
     
  3. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    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$
     
  4. Squiggle

    Squiggle Registered

    Joined:
    Aug 13, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Thanks Dave, I will give it a go!
     
  5. Squiggle

    Squiggle Registered

    Joined:
    Aug 13, 2013
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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 :)
     
  6. cPAndrewHodge

    cPAndrewHodge cPanel UI Developer
    Staff Member

    Joined:
    Dec 10, 2012
    Messages:
    52
    Likes Received:
    3
    Trophy Points:
    8
    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[ ]*$
     
  7. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    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.

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

Share This Page