Aks
Aks

Reputation: 5236

Need help understanding preg_match regular expression

A regular expression in preg_match is given as /server\-([^\-\.\d]+)(\d+)/. Can someone help me understand what this means? I see that the string starts with server- but I dont get ([^\-\.\d]+)(\d+)'

Upvotes: 0

Views: 789

Answers (4)

Toto
Toto

Reputation: 91385

Here is the explanation given by the perl module YAPE::Regex::Explain

The regular expression:

(?-imsx:server\-([^\-\.\d]+)(\d+))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  server                   'server'
----------------------------------------------------------------------
  \-                       '-'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^\-\.\d]+               any character except: '\-', '\.', digits
                             (0-9) (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

Upvotes: 1

sergioviniciuss
sergioviniciuss

Reputation: 4696

[ ] -> Match anything inside the square brackets for ONE character position once and only once, for example, [12] means match the target to 1 and if that does not match then match the target to 2 while [0123456789] means match to any character in the range 0 to 9.

- -> The - (dash) inside square brackets is the 'range separator' and allows us to define a range, in our example above of [0123456789] we could rewrite it as [0-9].

You can define more than one range inside a list, for example, [0-9A-C] means check for 0 to 9 and A to C (but not a to c).

NOTE: To test for - inside brackets (as a literal) it must come first or last, that is, [-0-9] will test for - and 0 to 9.

^ -> The ^ (circumflex or caret) inside square brackets negates the expression (we will see an alternate use for the circumflex/caret outside square brackets later), for example, [^Ff] means anything except upper or lower case F and [^a-z] means everything except lower case a to z.

You can check more explanations about it in the source I got this information: http://www.zytrax.com/tech/web/regex.htm

And if u want to test, u can try this one: http://gskinner.com/RegExr/

Upvotes: 2

Tchoupi
Tchoupi

Reputation: 14681

^ means not one of the following characters inside the brackets

\- \. are the - and . characters

\d is a number

[^\-\.\d]+ means on of more of the characters inside the bracket, so one or more of anything not a -, . or a number.

(\d+) one or more number

Upvotes: 1

Nikola Malešević
Nikola Malešević

Reputation: 1858

Here's the explanation:

# server\-([^\-\.\d]+)(\d+)
# 
# Match the characters “server” literally «server»
# Match the character “-” literally «\-»
# Match the regular expression below and capture its match into backreference number 1 «([^\-\.\d]+)»
#    Match a single character NOT present in the list below «[^\-\.\d]+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
#       A - character «\-»
#       A . character «\.»
#       A single digit 0..9 «\d»
# Match the regular expression below and capture its match into backreference number 2 «(\d+)»
#    Match a single digit 0..9 «\d+»
#       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

You can use programs such as RegexBuddy if you intend to work with regexes and are willing to spend some funds.

You can also use this free web based explanation utility.

Upvotes: 1

Related Questions