0

I need to clean up a database containing strings like this:

<a href="/members/a/" title="a">a</a> ist der Gruppe <a href="/groups/3106/">a</a> beigetreten

or also

<a href="/members/a/" title="a">a</a> ist der Gruppe <a href="/groups/3106_123/">a</a> beigetreten

or also

<a href="/members/a/" title="a">a</a> ist der Gruppe <a href="/groups/3106_A/">a</a> beigetreten

or also

<a href="/members/a/" title="a">a</a> ist der Gruppe <a href="/groups/3106A/">a</a> beigetreten

I only need the numeric part after /groups/, so in this examples, only "3106". The number is of variable length.

Stripping the first part away is easy:

SUBSTRING(field, locate('groups/', field)+7)

but how to strip the rest away?

4
  • I think you might have to do a little bit in code, a regex would help you find what you need in MySQL but you can't extract from it. Commented Apr 14, 2015 at 13:21
  • I would use a scripting language for this. Commented Apr 14, 2015 at 13:21
  • Thanks guys. I would also appreciate an answer with a PHP-regex so I could write a small program with that. Commented Apr 14, 2015 at 13:22
  • 1
    Here you are: (?<=\/groups\/)[0-9]+. See regex101.com/r/zG8pL9/1 Commented Apr 14, 2015 at 13:31

2 Answers 2

1

Since you are asking for a PHP regex, here you are:

(?<=\/groups\/)[0-9]+

PHP sample code:

$re = "/(?<=\\/groups\\/)[0-9]+/"; 
$str = "<a href=\"/members/a/\" title=\"a\">a</a> ist der Gruppe <a href=\"/groups/3106A/\">a</a> beigetreten"; 
preg_match_all($re, $str, $matches);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this way using REGEX of MYSQL to select only numeric of your string column value

SELECT column_name_of_db
FROM db_name
WHERE column_name_of_db REGEXP '^(?<=\/groups\/)\d+';

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.