-1

I need some help from the RegEx and SQL nerds. ^^

I've a comment field in a table, which content looks like this:

What I need is a DISTINCT list of all user names - eg.

  • b.willis
  • p.fox
  • g.clooney

    CREATE TABLE IF NOT EXISTS `comments` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `comment` varchar(255) NOT NULL,
    PRIMARY KEY (`id`)
     ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
    
    INSERT INTO `comments` (`id`, `comment`) VALUES
    (1, 'test [name](p.fox)[/name]'),
    (2, 'another test [name](p.fox)[/name]'),
    (3, 'lalala [name](b.willis)[/name]'),
    (4, 'lulu [name](g.clooney)[/name]');
    

Thx!

Petra
  • 555
  • 1
  • 6
  • 20

2 Answers2

0

One way to approach this is using substring_index(). Assuming each comment has this structure and only has one name:

select distinct substring_index(substring_index(comment, '[name](', 2), ')[/name]', 1) as name
from comments
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
0

See the thread MySQL - Return matching pattern in REGEXP query

Also check How to do a regular expression replace in MySQL? and the referenced UDF implementation of RegExp. Take a look on the implementation of the UDF REGEXP_SUBSTR

Community
  • 1
  • 1
Zlatin Zlatev
  • 2,888
  • 1
  • 22
  • 30