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. -
$x = "The start text always precedes 5 times
-
of the end text.";
-
-
if ($x=~ /The(.*)times(\d+)/) {
-
-
print "X:$1."
-
}
-
Thanks
Jit
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
You referenced $1 in your code, but you should actually be referencing $2. Try changing it and see if that prints what you want.
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.
The . symbol is greedy. It will eat up your digit along with everything else.
Aren't you actually looking for /\b(\d)+\b/
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.
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\./
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"
}
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: -
#!/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
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"
}
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
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.
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
Sure, you could always match the first number and then use a lookahead assertion to match the next number.
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
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
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.
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 - while ($string =~ m/regex/g) {
-
print "Found '$&'. Next attempt at character " . pos($string)+1 . "\n";
-
}
RonB 589
Expert Mod 512MB - #!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
use 5.010;
-
-
my $x = "The start text always precedes 5 times
-
of the 6 end text.";
-
-
while ( $x =~ /.+?(\d+)./g ) {
-
say $1;
-
}
Hi RonB,
Thanks for your response.
I tried but getting only 5:
z:5
X:
Y:
Am not getting number 6.
RonB 589
Expert Mod 512MB
You didn't run the test script I provided.
E:\TEMP\test>type perl-2.pl - #!/usr/bin/perl
-
-
use strict;
-
use warnings;
-
use 5.010;
-
-
my $x = "The start text always precedes 5 times
-
of the 6 end text.";
-
-
while ( $x =~ /.+?(\d+).+/g ) {
-
say $1;
-
}
-
-
E:\TEMP\test>perl-2.pl
5
6
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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"
|
by: VJ |
last post by:
Is there any known component that would convert a value entered as 40,000 to
Forty Thousand..
Thanks
VJ
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |