Help me about regular expression.

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by duongca69, Apr 13, 2013.

  1. duongca69

    duongca69 New Member

    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Hello guys
    I need to create a regular expression to valid a string with format: {mediagallery id=[1,8,2,3,4,5,10,6,11,7,...,124]}
    If you can, please help me!
    Thanks
     
  2. Skizzerz

    Skizzerz Contributors Staff Member Contributors

    Messages:
    2,929
    Likes Received:
    117
    Trophy Points:
    63
    Can you give a few actual examples of what you are matching? From that one snippet I can't tell if id is supposed to be a comma-separated list, or if id is supposed to be a single number and those numbers are all valid values for it.
     
  3. lompocus

    lompocus New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Do you mean that you want to determine whether a string's format is "legal," according to the format you posted? Well, assuming those ellipses aren't actually there....

    Break down the problem just as you would any other. You have opening and closing brackets of two kinds, right? If you're only checking whether something is enclosed in brackets, then this might work:

    /{.*}/

    Now we have a word. You can try

    /{mediagallery\wid

    and add stuff as necessary. I'm still new to this. Hope this was remotely helpful. Start by trying to validate [1, 2, 3, ..., 123]. Keep on adding stuff until you successfully validate everything.

    Presumably you want to retrieve the numbers. What I do (reading from perl, so number->string is easy, dunno about anything else) is enclose everything in quotes, then you can loop through pattern checking with

    m/'(.*)?'/g or m/'(.*)'/g or something similar

    which, presumably, destroys the string being checked in the process.

    (First post!)
     
  4. duongca69

    duongca69 New Member

    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Thank guys. Problems of me have been solved
     

Share This Page