473,509 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printing only the pattern that matches

5 New Member
I am trying to display only the line that ends with a . or , or ? that has the in the line that ended with .|,|?

So here is my pattern-matching operator:

m/the/ .. /(\.|\?|\,)$/im

I am not sure if that is the correct pattern to look for, but if it is how do I extract just what matches that?

For example if I had

I wish it was the that dog barked,
but it wasn't.
It was a cat.

I need to only print "the dog barked,"
Jan 17 '07 #1
5 1183
prasadbabu
5 New Member
keirnna,

What you have done here is correct /(\.|\,|\?)$/im.

But i am not sure what you are doing here (/the/../).

You can also use the character class to accomplish the same:

/([.,?])$/im

Regards,
Prasad
Jan 17 '07 #2
prasadbabu
5 New Member
I hope this might help you,

Example:

use strict;
use warnings;

my $string = "I wish it was the that dog barked,
but it wasn't.
It was a cat?";

while ($string =~ m/([.\,?])$/gim) {
print "line ended with <$1>\n";
}
Jan 17 '07 #3
miller
1,089 Recognized Expert Top Contributor
Or if you want exactly what you stated:

Expand|Select|Wrap|Line Numbers
  1. my $string = "I wish it was the dog that barked,\nbut it wasn't.\nIt was a cat?";
  2.  
  3. while ($string =~ m/(the.*?[.,?])$/gim) {
  4.     print "line ended with <$1>\n";
  5. }
  6. # Outputs: line ended with <the dog that barked,>
  7.  
Jan 17 '07 #4
KevinADC
4,059 Recognized Expert Specialist
Hi Miller,

Seems we have been working on the same question, allbeit on different forums:

Expand|Select|Wrap|Line Numbers
  1. my $file = $ARGV[0];    
  2.  
  3. open(INPUT, $file)   
  4.     or die "Couldn't open $file for reading: $!\n";   
  5.  
  6. while (<INPUT>) {   
  7.    if (m/(it.*[?,.])$/i) {  
  8.       print "$1\n";  
  9.    }  
  10. }   
  11. close(INPUT);
  12.  
(from other forum)
Here is the file I want to scan:

Tim created the Module List in August 1994 and maintained it manually till
April 1996. By that time Andreas had implemented the Perl Authors Upload
Server (PAUSE) and it was happily feeding modules through to the CPAN archive sites.
Since PAUSE held a database of module information which could be maintained by module authors
it made sense for the module listing part of the Module List to be built
from that database. In April 1996 Andreas took over the automatic posting of
the Module List and I now maintain the other parts of the text. We plan to add
value to the automation over time.
Jan 18 '07 #5
keirnna
5 New Member
Yes I also posted on another forum. I don't see what the problem is. Thanks for the help so far.
Jan 19 '07 #6

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

Similar topics

1
1994
by: Dean A. Hoover | last post by:
I've got a 4 line message that looks like this: Encountered "<EOF>" at line 1, column 7. Was expecting one of: "=" ... "]]" ... and want to get the line and column numbers from it. This is...
5
3312
by: Kelmen Wong | last post by:
Greeting, I want to extract all "" from a string, what pattern should I used? eg. = - return array or test1, or test2
4
9703
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
5990
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
4
3588
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
3
1583
by: mehdi_mousavi | last post by:
Hi folks, Consider the following JavaScript function: function removeParam(str, name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)'); rgx.global = true; rgx.ignoreCase =...
19
3153
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
4
2103
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
3
6085
by: eBob.com | last post by:
Is there a regex pattern which will match a VB.Net string? I.E. a regex which matches ... "this is a ""vb.net"" string" (I don't want three matches in this case, I want one.) I've come up...
0
7237
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
7137
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
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.