-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
  • 565
  • 1
  • 7
  • 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,242,037
  • 58
  • 646
  • 786
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
  • 3,034
  • 1
  • 24
  • 32