473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to extract number between two words?

9
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.

Expand|Select|Wrap|Line Numbers
  1. $x = "The start text always precedes 5 times
  2.               of the end text.";
  3.  
  4. if ($x=~ /The(.*)times(\d+)/) {
  5.  
  6. print "X:$1."
  7. }
  8.  
Thanks
Jit
Jan 11 '11 #1

✓ answered by Rabbit

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\./

19 3263
numberwhun
3,509 Expert Mod 2GB
You referenced $1 in your code, but you should actually be referencing $2. Try changing it and see if that prints what you want.
Jan 11 '11 #2
sjit
9
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.
Jan 11 '11 #3
Rabbit
12,516 Expert Mod 8TB
The . symbol is greedy. It will eat up your digit along with everything else.

Aren't you actually looking for /\b(\d)+\b/
Jan 11 '11 #4
sjit
9
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.
Jan 11 '11 #5
Rabbit
12,516 Expert Mod 8TB
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\./
Jan 11 '11 #6
sjit
9
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"
}
Jan 11 '11 #7
numberwhun
3,509 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $x = "The start text always precedes 5 times of the end text.";
  7.  
  8. if ($x=~ /(\d+)/) {
  9.     print "X: $1\n";
  10. }
  11.  
Regards,

Jeff
Jan 11 '11 #8
sjit
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"
}
Jan 11 '11 #9
sjit
9
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
Jan 11 '11 #10
Rabbit
12,516 Expert Mod 8TB
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.
Jan 11 '11 #11
sjit
9
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
Jan 12 '11 #12
numberwhun
3,509 Expert Mod 2GB
Sure, you could always match the first number and then use a lookahead assertion to match the next number.
Jan 12 '11 #13
Rabbit
12,516 Expert Mod 8TB
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
Jan 12 '11 #14
sjit
9
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
Jan 12 '11 #15
Rabbit
12,516 Expert Mod 8TB
Is global mode enabled in your regex settings? I read that just putting /g will turn on global mode but if it's global, it should be matching.
Jan 12 '11 #16
Rabbit
12,516 Expert Mod 8TB
I was reading up on the perl regex engine and it turns out that to get all matches, you have to do something like this
Expand|Select|Wrap|Line Numbers
  1. while ($string =~ m/regex/g) {
  2.   print "Found '$&'.  Next attempt at character " . pos($string)+1 . "\n";
  3. }
Jan 12 '11 #17
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use 5.010;
  6.  
  7. my $x = "The start text always precedes 5 times
  8. of the 6 end text.";
  9.  
  10. while ( $x =~ /.+?(\d+)./g ) {
  11.     say $1;
  12. }
Jan 12 '11 #18
sjit
9
Hi RonB,
Thanks for your response.
I tried but getting only 5:
z:5
X:
Y:
Am not getting number 6.
Jan 12 '11 #19
RonB
589 Expert Mod 512MB
You didn't run the test script I provided.

E:\TEMP\test>type perl-2.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use 5.010;
  6.  
  7. my $x = "The start text always precedes 5 times
  8. of the 6 end text.";
  9.  
  10. while ( $x =~ /.+?(\d+).+/g ) {
  11.     say $1;
  12. }
  13.  
  14.  
E:\TEMP\test>perl-2.pl
5
6
Jan 12 '11 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: levent | last post by:
What is an elegant way (using std::stream's) to extract number of white-space separated entries in a given line of a formatted text file? e.g.: Take this section of a file, 1 2.78 4 5 -0.003...
5
by: chuck | last post by:
Hello, I am trying to figure out how to get a number (always an int) from a string. so if i have something like <input type="text" id="product13" name="product13" onblur="return...
2
by: Branden | last post by:
hi guys, i was wondering if it is possible to extract selected words in a field to be put in different fields automatically. Do i have to write the code in vb? This is what im trying to do....
3
by: Maya | last post by:
Hello guys, Is there an easy way to extract individual words that form a string and store them in variables like in this example: String "how are you?" My result would be: var1 = "how"
5
by: VJ | last post by:
Is there any known component that would convert a value entered as 40,000 to Forty Thousand.. Thanks VJ
5
by: akelly_image | last post by:
Okay, if anyone could toss me some idea's here, please bare with my noobish questions, I just picked up VB2005 Pro about a week ago. ( no prior VB at all ) Here's my issue.. I'm pulling...
5
by: 1965 | last post by:
Hi,all. I want to extract all words ( not including punctuations) from a text and convert all individual words into hyperlinks directing a dictionary site. When clicking on any word will go to the...
3
by: thesinnerishere | last post by:
this is regarding the last number standing problem. it requires the number in an array to be extracted at a specified position . then the array needs to traverse through the right position of the...
0
by: wbw | last post by:
I am trying to extract capitalized words from text in Excel. I have a list of a combination of brands and products and I am trying to extract out the product attribute from the text. Since the text...
2
by: MaryJolly | last post by:
I want to extract number part from a string. I am doing project in VB6 and my MSFlexGrid control contains certain codes in the first column.eg. EC101,CS104,etc. I want to extract the number part from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.