How to extract number between two words?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sjit
    New Member
    • Jan 2011
    • 9

    #1

    How to extract number between two words?

    Hi There,
    I am trying to extract the number between two words.
    Below is the code which am using but not getting any output.
    Could you please help me to get the output.

    Code:
    $x = "The start text always precedes 5 times
                  of the end text.";
    
    if ($x=~ /The(.*)times(\d+)/) {
    
    print "X:$1."
    }
    Thanks
    Jit
    Last edited by numberwhun; Jan 12 '11, 05:05 PM. Reason: Please use code tags!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    You referenced $1 in your code, but you should actually be referencing $2. Try changing it and see if that prints what you want.

    Comment

    • sjit
      New Member
      • Jan 2011
      • 9

      #3
      Thanks numberwhun for your response.
      If I don't use (\d+)then am gettign the right output for $1 that is "start text always precedes 5"
      anyway I used $2 and still not getting the output.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        The . symbol is greedy. It will eat up your digit along with everything else.

        Aren't you actually looking for /\b(\d)+\b/

        Comment

        • sjit
          New Member
          • Jan 2011
          • 9

          #5
          My Intention is to capture the number between two words. In this example between The and times. That is why am using
          /The(.*)times(\d +)/. I also tried with \b but still no output. if I remove \d, them am getting the text.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            I'm not sure why (\d+) is working unless it's seeing the period as a numeric character. But the reason it doesn't work without it is because you didn't account for the period at the end of the sentence so it's not matching. So really it should be /The(.*)times\./

            But that .* is too greedy. Try /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*times\./

            Comment

            • sjit
              New Member
              • Jan 2011
              • 9

              #7
              Thanks Rabbit. it is working now. but I have to remove "\." and $2 is responding but not $1.(any reason?)
              The modified code is:
              if ($x=~ /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*times/) {
              To have more understanding of the code I put another number (6) in 2nd line and trying to extract (6) by $3 but its not giving any output.
              **************
              Could you help me on this please.
              $x = "The start text always precedes 5 times
              of the 6 end text.";
              if ($x=~ /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*text/) {
              print "\n X:$3\n"
              }

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                My mistake. Had to wake up and rethink what was going on. $2 would not have it either because the number was contained in $1. You are grabbing way to much. You just want the number. So why not either use what Rabbit has given you, or try this sample, which does grab the number for you:

                Code:
                #!/usr/bin/perl
                
                use strict;
                use warnings;
                
                my $x = "The start text always precedes 5 times of the end text.";
                
                if ($x=~ /(\d+)/) {
                    print "X: $1\n";
                }
                Regards,

                Jeff

                Comment

                • sjit
                  New Member
                  • Jan 2011
                  • 9

                  #9
                  I got it.
                  I have to put /s.
                  Here is the code:
                  $x = "The start text always precedes 5 times
                  of the 6 end text.";
                  if ($x=~ /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*text/s){
                  print "\n X:$3\n"
                  }

                  Comment

                  • sjit
                    New Member
                    • Jan 2011
                    • 9

                    #10
                    Sorry I was too excited to jump into conclusion.
                    $3 is not fetching anything. but$2 is fetching 5.
                    Am still not able to get $3=6

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      I don't know how perl implements regex so I don't know what $3 and what /s does but if you're trying to return all number matches, how come you can't use m/\b\d+\b/g

                      However, if you wish to use the more precise expression and return all number matches, you would need to get the submatches from your expression wouldn't you? Unfortunately I don't know how you go about doing that in perl though.

                      Comment

                      • sjit
                        New Member
                        • Jan 2011
                        • 9

                        #12
                        I can use m/\b\d+\b/g to get all the matching numbers but need to use between two words from multiple lines.
                        That is why am using this code to minimize the search area between two words:
                        $x = "The start text always precedes 5 times
                        of the 6 end text.";
                        if ($x=~ /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*text/){
                        print "\n X:$2\nY:$3\n"
                        }
                        Any suggestion to get the values for X & Y

                        Comment

                        • numberwhun
                          Recognized Expert Moderator Specialist
                          • May 2007
                          • 3467

                          #13
                          Sure, you could always match the first number and then use a lookahead assertion to match the next number.

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            So you just want numbers that aren't at the start or end of the string? What about m/[^\A]\b(\d+)\b[^\Z]/g

                            Comment

                            • sjit
                              New Member
                              • Jan 2011
                              • 9

                              #15
                              Thanks Rabit for your patience with me and response to my queries.
                              I got only one output. Please see below;
                              ***********
                              $x = "The start text always precedes 5 times
                              of the 6 end text.";

                              #if ($x=~ /The([A-Za-z\s])*\b(\d+)\b([A-Za-z\s])*text/g) {
                              if ($x=~ m/[^\A]\b(\d+)\b[^\Z]/g) {
                              print "\nZ=$1 X:$2\nY:$3\n"
                              }
                              *************
                              Output:
                              Z=5 X:
                              Y:
                              ---------
                              This time got a response for $1 but still didn't get number 6.
                              Thanks

                              Comment

                              Working...