1

When I do

SELECT resrev_meta FROM resourcerevs WHERE resrev_meta LIKE '%attr%';

I get results like this

<attr><fileid>131</fileid></attr> 
<attr><fileid>326</fileid><width>360</width><height>640</height></attr> 

Question

Is it possible to have a RegEx that would only output the number between <fileid> and </fileid>?

3
  • do you want only the fieldid even if there are multiple attributes ? Commented Jun 26, 2012 at 17:04
  • Yes, only the fileid if possible. Commented Jun 26, 2012 at 17:06
  • I had a similar problem and was elated to find out that mysql supported XPATH expressions. Commented Jun 26, 2012 at 17:12

3 Answers 3

5

Regex is probably not what your looking for. mysql supports xpath expressions.

This should give you what you need:

SELECT ExtractValue(resrev_meta,'//fileid')  AS fileid 
     FROM resourcerevs 
     WHERE resrev_meta LIKE '%attr%';

http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html#function_extractvalue

mysql> SELECT
    ->   ExtractValue('<a>ccc<b>ddd</b></a>', '/a') AS val1,
    ->   ExtractValue('<a>ccc<b>ddd</b></a>', '/a/b') AS val2,
    ->   ExtractValue('<a>ccc<b>ddd</b></a>', '//b') AS val3,
    ->   ExtractValue('<a>ccc<b>ddd</b></a>', '/b') AS val4,
    ->   ExtractValue('<a>ccc<b>ddd</b><b>eee</b></a>', '//b') AS val5;

+------+------+------+------+---------+
| val1 | val2 | val3 | val4 | val5    |
+------+------+------+------+---------+
| ccc  | ddd  | ddd  |      | ddd eee |
+------+------+------+------+---------+
Sign up to request clarification or add additional context in comments.

Comments

3

No, not using regex. Mysql doesn't support regex-based replacement or group matching etc.

The only regex support is RLIKE or REGEXP (they are synonyms), which is used to match a column value.

Comments

3

No, but you can do something hacky like this if you want (pardon the long lines):

SELECT
  SUBSTRING(
    resrev_meta,
    INSTR(resrev_meta, '<fileid>') + LENGTH('<fileid>'),
    INSTR(resrev_meta, '</fileid>') - INSTR(resrev_meta, '<fileid>') - LENGTH('<fileid>')) AS fileid
FROM resourcerevs WHERE resrev_meta LIKE '%attr%';

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.