tags:

views:

48

answers:

5

What does this regular expression mean?

preg_match("/^obj(\d+)\-{0,1}(|mi\d{0,1}|critical|questionText|answerText\-{0,1}\d+)$/", $k,  $a)

the preg_match is a php function that should translate it

+1  A: 

^obj(\d+)-{0,1} means that the string is the start of a line and begins with obj, followed by a number of at least 1 digit, then there might be a - sign.

(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) means that the text is one of the following:

  • nothing
  • mi which might be followed by a digit
  • critical
  • questionText
  • answerText which can be followed by one - sign, and after that a number of at least 1 digit

Then there's the end of the line. The search is case sensitive.

Alexander
A: 

IT will match a string that starts with obj, followed by a numer, an optional dash, followed by either

  • nothing
  • mi followd by a number
  • critical
  • questionTest
  • answerText followed by an optional - which is in turn followed by a number (not optional) and then the end of the string.

Examples:

  • obj932-mi21
  • obj3124critical
  • obj1-answerText-86
  • obj654answerText8
  • obj23-
phant0m
A: 

It matches a certain format for lines that contain, for example:

obj1-mi5
obj2critical
obj3-questionText
obj4answerText-0

Do these patterns look familiar?

preg_match will find that pattern in $k and store matches in $a. Do the following to see what it found.

print_r($a);
Jason McCreary
A: 

I'd firstly suggest finding a regex tutorial and reading up on how they are structured. This isn't a particularly complciated regex so you should be able to figure it out with some bookwork and it will mean you won't have to ask abotu the next regex you come across. ;-)

What the regex means broken down is as follows:

^ - matches the beginning of the string.

obj - this is literal text so will match those characters at the beginning of the string

(\d+) - this will match one or more digits (0-9) and the brackets will mean they are captured in such a way as they could be used after the parsing.

-{0,1} - this will match 0 or 1 "-" characters.

(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) - again the brackets will capture this as a group. The "|" is used as an "or" so it will match any of the separated values. Although I'm not sure I think that the fact it starts with a "|" might mean it will match an empty string.

mi\d{0,1} - matches the miteral string mi followed by 0 or 1 digits.

critical, questionText - these are both literal options that match the exact text

answerText-{0,1}\d+ - this will match the literal string answerText followed by an optional "-" and one or more digits.

$ - the string must end immediately after the previous match.

I hope that makes sense to you. As I say, check some tutorials and docs if you need more help. :)

Chris
+3  A: 

This expression, commented would look like

^                  // start of line
 obj               // literal obj
(\d+)              // one or more digits (0-9), captured in a group
 -{0,1}            // optional dash 
(                  // start second capturing group
                   // nothing
    |              // ... OR ...
    mi\d{0,1}      // literal mi, followed by an optional digit
    |              // ... OR ...
    critical       // literal critical
    |              // ... OR ...
    questionText   // literal questionText
    |              // ... OR ...
    answerText     // literal answerText
       -{0,1}      // optional dash 
       \d+         // one or more digits (0-9)
)                  // end of capturing group
$                  // end of line

An example of what is matches would be

obj1000-critical or obj1000answerText-100

Jens