Using regex to extract variables from a plain-text form letter?

Posted by Yaaqov on Stack Overflow See other posts from Stack Overflow or by Yaaqov
Published on 2010-04-12T15:45:23Z Indexed on 2010/04/12 16:03 UTC
Read the original article Hit count: 410

Filed under:
|
|
|

Hi - I'm looking for a good example of using Regular Expressions in PHP to "reverse engineer" a form letter (with a known format, of course) that has been pasted into a multiline textbox and sent to a script for processing.

So, for example, let's assume this is the original plain-text input (taken from a USDA press release):

WASHINGTON, April 5, 2010 - North American Bison Co-Op, a New Rockford, N.D., establishment is recalling approximately 25,000 pounds of whole beef heads containing tongues that may not have had the tonsils completely removed, which is not compliant with regulations that require the removal of tonsils from cattle of all ages, the U.S. Department of Agriculture's Food Safety and Inspection Service (FSIS) announced today.

For clarity, the fields that are variables are highlighted below:

[pr_city=]WASHINGTON, [pr_date=]April 5, 2010 - [corp_name=]North American Bison Co-Op, a [corp_city=]New Rockford, [corp_state=]N.D., establishment is recalling approximately [amount=]25,000 pounds of [product=]whole beef heads containing tongues that may not have had the tonsils completely removed, which is not compliant with regulations that require [reason=]the removal of tonsils from cattle of all ages, the U.S. Department of Agriculture's Food Safety and Inspection Service (FSIS) announced today.

How could I efficiently extract the contents of the

  • pr_city
  • pr_date
  • corp_name
  • corp_city
  • corp_state
  • amount
  • product
  • reason

fields from my example?

Any help would be appreciated, thanks.

© Stack Overflow or respective owner

Related posts about regex

Related posts about parsing