?
Solved

Regular Expressions- Making a Backreference

Posted on 2008-11-17
3
Medium Priority
?
393 Views
Last Modified: 2012-05-05
I'm trying to make a Regex with backreferences- and I'm not very familiar with backreferencing.
Here's an example that I'm running the regex against.

"The legal description is Township 27 South, Range 23 East, Sections 1, 10, 11, 12, 15, 19, 20,
21, and 22; Township 27 South, Range 24 E., Section 6; Township 26 South, Range 23 East, Sec. 1,
2, 11, 13, 14, 24, 25, and 36; Township 26 South, Range 24 East, Sec 31; and Township 25 South,
Range 23 East, Sections 25, 35, and 36.  The survey area..."

In which I want returned:
Township 27 South, Range 23 East, Sections 1, 10, 11, 12, 15, 19, 20, 21, 22
Township 27 South, Range 24 East, Section 6
Township 26 South, Range 23 East, Sections 1, 2, 11, 13, 14, 24, 25, 36
Township 26 South, Range 24 East, Section 31
Township 25 South, Range 23 East, Sections 25, 35, 36

I'm having trouble in getting all of the numeric entries under 'Sections'and I guess I need to use
a backreference, but am unsure exactly how to do so.

Here are the Regex's I have so far.
(?<township>\btownship\s?\d{1,2}\s?(s(outh)?|n(orth)?))(?:,?\s?)
(?<range>\brange\s?\d{1,2}\s?(e(ast)?|w(est)?))(?:.?,?\s?)
(?<section>\bsec(\.?)(tion)?(s?)\s?\d{1,2})

which returns:
Township 27 South, Range 23 East, Sections 1
Township 27 South, Range 24 E., Section 6
Township 26 South, Range 23 East, Sec. 1
Township 26 South, Range 24 East, Sec 31
Township 25 South, Range 23 East, Sections 25
Township 13 North, Range 16 East, sec. 32

How do I construct the Regex for the <section> group to match all numbers that fall within this
area?
Thanks for your help!
0
Comment
Question by:montarch
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 22977312
Consider this example:
http://www.myregextester.com/?r=8b558af7

I modified your pattern, see if it fits your source.  Note that instead of \s? I changed it so \s* to make a CRLF that you had in that location within the source data, that may need to be required elsewhere based on the variance in your source text.

See this example for a more restrictive definition of Section:
http://www.myregextester.com/?r=20b9df1c
0
 

Author Closing Comment

by:montarch
ID: 31517496
Absolutely PERFECT!  Once again, you've saved and humbled me.

Thanks again!

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22977693
Glad I could help.  Thanks for the question and the points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question