#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Pierrefonds, QC Canada
    Posts
    205
    Rep Power
    11

    Write and use a regular expression in PHP


    Hi there!

    I wish to extract from texts some chunks which always are build on the same way or, if you prefer, on the same "pattern".
    So, it will be useful for me to know how to write and use a regex which will be able to find as a whole:

    - a first group of characters: upper or lowercase, spaces (always followed by a letter), some quotation marks and/or dashes but no numbers.
    - a space always followed by a number (may be used as a kind of "delimiter" ??? between the two groups)
    - a second group of characters: numbers, dashes (always), interrogation mark (sometimes) but no spaces and no letters.

    Examples :

    L'Arbresle-sur-le-Lac 2345-6789

    or

    Saint-Denis l'…glise 1234-?

    Select from, and print to, a MySQL Table.
    This regex will be used in a PHP script to extract the text matching the regex and write it in another column of the Table.

    I am not familiar at all with preg_xxxx and don't understand how to write my query to use the regex...

    :-(

    Many thanks in advance for your help and/or advices!
    Last edited by Germaris; February 6th, 2013 at 06:46 AM.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Pierrefonds, QC Canada
    Posts
    205
    Rep Power
    11
    Thanks for replying!

    Ha, ha, ha !!! Good advice which makes sense.

    However, it's urgent to solve my problem and, sorry, I don't have the time for starting to study something some people take weeks or months to learn...
    I assume you'll understand that.

    And, believe me, even at the age of 72, I'm not a lazy person!
    I already wrote:

    Code:
    ([[:alpha:]-\'\s]+) ([0-9]+)-([0-9]+|\?)
    but it doesn't work.
    MySQL returns:
    #1139 - Got error 'invalid character range' from regexp
    Last edited by Germaris; February 6th, 2013 at 08:23 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    maybe show the sql query so we know what you are doing..
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Pierrefonds, QC Canada
    Posts
    205
    Rep Power
    11
    Originally Posted by paulh1983
    maybe show the sql query so we know what you are doing..
    Thanks for replying, paulh1983!

    Here is the query:

    Code:
    SELECT ID, longName, notes
    FROM `_a_listregex`
    WHERE notes REGEXP '([a-zA-Z][\-\…\ť\Ť\'\s]+) ([0-9]+)-([0-9]+|\\?)'
    ORDER BY ID
    Meanwhile, I worked on this problem.
    Now, it works, but not as expected.

    I know that the query should return at least 2500 results.
    It only returns a mere 800...

    Now, my problem is to find out why it ignores some chunks and not others...
    Eliminating escaped accented letters doesn't change returned results.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Pierrefonds, QC Canada
    Posts
    205
    Rep Power
    11
    I gave you the query as requested...

    And now, what?
    Just tell me if you give up...
    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo