is there a way to check if the givin value is a regex ???

Discussion in 'PHP' started by free-designer, Dec 1, 2010.

  1. #1
    Hey guys,

    is there any way to check if the givin value is a regex using a regex

    more details

    
    $value = "/^(.*)$/";
    $check = preg_match("[missing regex]", $value, $match);
    
    PHP:
    it's pretty simple now, i want someone to give me a regex to check a value if it's a regex :)

    if there any other better way of doing this please share it

    Thanks a lot
     
    free-designer, Dec 1, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Well using a regex for that could become complicated in the long run, would be simpler to make use of php's existing functions to come up with some sort of validation, e.g. to start you can check if the start and ending character are the same then check if their a valid delimiter.

    Also take alook at preg_quote() - may become useful.
     
    danx10, Dec 1, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yea i thought of that too, i thought that it would be complicated, i guess ur opinion is mush better, i'll keep it as you told me ;) ,, Thanks for helping me with my script
     
    free-designer, Dec 1, 2010 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    There is no such thing as a valid regular expression, this is why they are valid and invalid for certain cases. You can however, check if the syntax is correct be doing the following:

    1) Check for valid delimiters as dan said.
    2) Check for valid modifiers.
    3) Check for invalid and closed meta characters.
    4) Check for valid character classes.
     
    ThePHPMaster, Dec 1, 2010 IP