Regular Expression search and replace on Autoblogged Plugin

529 views Asked by At

I'm using Autoblogged to pull a feed in as a blog post. I need to create a reg expression to convert the title of the item to things I can use as meta data. I've attached a screen of the backend I have access to. Any help would be greatly appreciated!

Here are examples of the title from the feed.

Type One Training Event New New Mexico, WY November 2012

Type Two Training Event Seattle, WA November 2012

I need that to become this:

<what>Type One Training Event</what> <city>New New Mexico</city>, <%state>WY</state> <month>November</month> <year>2012</year>

<what>Type Two Training Event</what> <city>Seattle</city>, <state>WA</state> <month>November</month> <year>2012</year>

Essentially says take whatever is before the word event and make that "what"

Take anything after the word event and before the comma and make that "city"

Take the two letters after the comma and make that "state"

Take the last two words and make em month and year

Autblogged backend:enter image description here

2

There are 2 answers

4
AutoBlogged On BEST ANSWER

We actually have an email in queue to respond to you directly once we get our v2.9 update out. The update fixes a bug in the regex feature but I thought I would go ahead and comment here so this question isn't just left open.

The ability to extract info from a feed is one of the coolest and most powerful features of AutoBlogged and this is a perfect example of what you can do with those features.

First of all, here are the regex patterns you would use:

What: (.*)\sTraining\sEvent

City: Training\sEvent\s([^,]*)

State: .*,\s([A-Z]{2})

To use these, you create new custom fields in the feed settings. Note that the custom fields also use the same syntax as the post templates so you can use the powerful regex function to extract info from the feed. This is how the fields should look:

Custom Fields

Once you create these custom fields you can use them in your post templates and they will be added as custom fields to your post in WordPress.

Once you have these custom fields set up, you can use them in your post template as %what%, %city% or %state_code%. As I mentioned before this will also create custom fields on your blog post in WordPress as well. If you don't want that, you can just use %regex("%title%", "(.*)\sTraining\sEvent", "1")% instead of %what% directly in your post template.

Quick explanation of the syntax: If you use %regex("%title%", "(.*)\sTraining\sEvent", "1")% it means this:

  • Get this info from the %title% field
  • Use the regex pattern (.*)\sTraining\sEvent
  • Use match reference 1, the (.*) part.
4
mathematical.coffee On

Perhaps match:

^(.* Event) (.*), ([A-Z]{2}) +(?i(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)) +((?:19|20)\d{2})\b

EDIT: re your comment, it looks like you have to surround your regex in delimiters. Try:

/insert_above_regex_here/

If you want case-insensitive, then do:

/insert_above_regex_here_but_remove_(?i_and_matching_)/i

However if you do case-insensitive, your state ([A-Z]{2}) will also match two lower-case letters. If this is OK then go for it. You culd also try changing that part of the regex to (?-i([A-Z]{2})) which says "be case-sensitive for this part", but it depends on whether that engine supports it (don't worry, you'll get an error if it doesn't).

Then replace with:

<what>$1</what> <city>$2</city>, <state>$3</state> <month>$4</month> <year>$5</year>

I'm not sure what flavour of regex that interface has so you might not be able to do the (?i bit in the Month regex (it just makes that bit case insensitive) -- you'll just have to be careful then to write your months with one capital letter and the rest lowercase, or you can modify the regex to allow upper-case too.