This plugin allows a user to use a REGEX to extract a string and set it as the value of a field. This is helpful if you'd like to extract a value from within a specific html node.

Comments

grasmash’s picture

StatusFileSize
new938 bytes

Attaching file.

grasmash’s picture

StatusFileSize
new1.48 KB

This plugin may be growing a bit too niche, but I still think it could be helpful to many people. I've added a few more options. Namely:

- Added ability to set cascading regexs. I.E., if extraction fails, an alternate regex will be used to attempt extraction.
- Added setting to choose fallback behavior if all regexs fail.

This plugin is really useful if you need to comb through a specific XML node that isn't always consistently formatted.

martysteer’s picture

I think this is a great addition! using backreferences in REGEX is very handy when parsing non-standard XML schema.
For example, I am consuming publisher journal feeds. Some publishers' xml feeds itemise individual journal/issue/page-numbers/doi in their XML feeds and are easy to grab with feeds_xpath_parser. However some put the item values into item descriptions, or concatenate them in various ways.

I am using this module now to extract individual values from xml feed value cases like:

a) American Nineteenth Century History, Volume 12, Issue 3, Page 377, September 2011
b) American Nineteenth Century History, Volume 12, Issue 3, Page 370-371, September 2011
c) American Nineteenth Century History, Volume 12, Issue 3, Page ebi, September 2011

with feed tamper plugin extract via regex:
/.+Volume ([0-9]+), Issue ([0-9]+), Page ([a-z0-9]+)-([a-z0-9]+),.+/

cascade to:
/.+Volume ([0-9]+), Issue ([0-9]+), Page ([a-z0-9]+),.+/

and fallback behaviour:
- leave field value as is.

This catches both single page entries (a)(c) and page ranges (b). I can use $1, $2, $3 or $4.

For the publisher feeds which have the values in specific XML elements, xpath parser pulls these out already, and since the tamper pattern doesn't match, the fallback behaviour is used.

Thankyou for this plugin! Without it I would have had to duplicate my feed importer to accommodate a given publishers XML quirks.

ps. One bug in #2 - add the default value to the fallback form element:
'#default_value' => isset($settings['fallback']) ? $settings['fallback'] : '',

dooug’s picture

I rolled the above tarball into a patch. I also included the fix that is mentioned in comment #3:

ps. One bug in #2 - add the default value to the fallback form element:
'#default_value' => isset($settings['fallback']) ? $settings['fallback'] : '',

twistor’s picture

Status: Needs review » Needs work

I read the patch, but I see a whole lot of options there that aren't explained. To be honest, I'm not entirely sure how this differs from normal capture group usage.

It looks very interesting, but I really need it laid out further.

Can I start requiring tests? We have a pretty simple unit test class, and lots of examples.

Alcaparra’s picture

I want to extract the mp3 link of a text and put into an audio field, that I created previously. But I don't understand what I have to put in the field "Subpattern to extract".

http://img706.imageshack.us/img706/6052/62129636.png
I use #4

martysteer’s picture

I think you need to enclose your regex in slashes:

/pattern/

Note sure if your patten is valid. Try the slashes first maybe?

technikh’s picture

I want to extract the first image in HTML code. I used below regex with "Subpattern to extract" $2
/<\s*?img\s+[^>]*?\s*src\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>/

The import returned error.
http://rubular.com/ shows the regex is right.

regex taken from http://stackoverflow.com/questions/1196570/using-regular-expressions-to-...

rerooting’s picture

This works great for me. Only bug I'm seeing is this:

Notice: Undefined index: 1 in feeds_tamper_extract_regex_subpattern_helper() (line 135 of /var/www/mow/htdocs/sites/all/modules/contrib/feeds_tamper/plugins/extract_regex.inc).

twistor’s picture

Status: Needs work » Closed (won't fix)

AFAICT, this can be accomplished with regex find/replace.

Alcaparra’s picture

"AFAICT, this can be accomplished with regex find/replace."

How?

twistor’s picture

You use regex capture groups () and can arrange them in with $1, $2, etc.