Link to home
Start Free TrialLog in
Avatar of montarch
montarch

asked on

Regular Expressions- Making a Backreference

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!
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of montarch
montarch

ASKER

Absolutely PERFECT!  Once again, you've saved and humbled me.

Thanks again!

Glad I could help.  Thanks for the question and the points.