SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Chaining REGEXP

  1. #1
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Chaining REGEXP

    Good afternoon all,

    I would like to somehow create an extensible query to 'chain' together a selection of regular expressions.

    My SQL skills are few and feeble, so I'd love some help.

    I currently have no schema built, so the world (or rather database) is your oyster.

    I was looking to have a table containing the data:-

    Data Table
    Code:
    id|data
    Pattern Table
    and a table contains patterns:-
    Code:
    id|name|pattern
    I would like the query to return the id field of the row which matches all the chained queries.

    Initially, I thought of this:-
    Code MySQL:
    SELECT id
    FROM DataTable
    WHERE DataTable.data
    REGEXP (
        SELECT pattern
        FROM PatternTable
        WHERE name = 'numeric'
    );

    Which doesn't really work.

    Each REGEXP call should pass on the value it matches to the next; eventually, if all matches, well, match, it would return the row id.

    Any ideas?

    Many thanks,
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of doing a subquery, can you do a conditional join?
    Code:
    SELECT p.postal_code
         , p.city
      FROM postal_codes p
    INNER
      JOIN `regexp` r_city
        ON r_city.name = 'city'
     WHERE p.city REGEXP r_city.pattern
    Would return all postal codes that match a given pattern on the city.
    MySQL v5.1.58
    PHP v5.3.6


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •