Results 1 to 7 of 7

Thread: Regex Help...Extracting distances from a line of text.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Regex Help...Extracting distances from a line of text.

    I have the following line of text

    3 Smith, Nathan I5 Elite 13-07.75 15-05.25
    and would like to extract the last distance (15-05.25). Its ok if I get both however. This is the pattern that I'm using:
    \d+\s*[.'-]\s*\d+[.']\s*\d+\s*[.*m chr(34)]*\s*[^-\d*]*\s*\d*[chr(34)]*\s[^\d]

    Unfortunatley, it only pulls the first distance (13-07.75).

    The pattern is designed to extract distances given in many different formats.

    Thank you.
    Adrian

  2. #2

    Re: Regex Help...Extracting distances from a line of text.

    So if this is essentially a CSV row (where, instead of commas, it is spaces), why not split on ' ', and grab the last item in the array?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Regex Help...Extracting distances from a line of text.

    try this:

    Code:
    Dim rx As New regex("\d{2}\-\d{2}\.\d{2}$")

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Regex Help...Extracting distances from a line of text.

    Quote Originally Posted by adrian1906 View Post
    I have the following line of text

    3 Smith, Nathan I5 Elite 13-07.75 15-05.25
    and would like to extract the last distance (15-05.25). Its ok if I get both however. This is the pattern that I'm using:
    \d+\s*[.'-]\s*\d+[.']\s*\d+\s*[.*m chr(34)]*\s*[^-\d*]*\s*\d*[chr(34)]*\s[^\d]

    Unfortunatley, it only pulls the first distance (13-07.75).

    The pattern is designed to extract distances given in many different formats.

    Thank you.
    Adrian
    .paul.'s solution will work if that's the format of the number you're using. If you want to match both then remove the $. You say that the distance can come in many different formats though so we'd need to know what formats you're expecting to give you a comprehensive answer.

    I'm curious as to your regex though. You say it matches 13-07.75? From a cursory glance I can't see how that's possible since you're looking for chr(34) and there's no occurrence of that in the string you've supplied.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Re: Regex Help...Extracting distances from a line of text.

    I'm seen distance represented quite a few ways in the document that I am parsing. Some examples include:

    2'0.5" 10'5.2" 10- 00.00 11-03.00 1.54m 20 - 6.6

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Re: Regex Help...Extracting distances from a line of text.

    "\d{2}\-\d{2}\.\d{2}" worked but I had to remove the '$" at the end. I will try to incorporate this in a more general pattern to pull down all distances that satisfy a pre-determined format.
    Last edited by adrian1906; May 10th, 2013 at 01:06 PM.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2010
    Posts
    148

    Re: Regex Help...Extracting distances from a line of text.

    I put the chr(34) inside square brackets.

    Some formats include:
    2'0.5"
    10'5.2"
    10- 10.25
    11- 03.00 (ie, strange spacing)
    1.54m
    20 - 6.6
    20ft6.6in
    20feet 6inches
    20f6in

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width