Perl: Regex, string matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Regex, string matching
# 1  
Old 01-12-2011
Perl: Regex, string matching

Hi,

I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries.

e.g. one of the logfile entry reads like this -

Code:
$str = "[latency][info] mpgw(BLUESOAPFramework): trans(24578291)[152.32.174.110]:Latency: 0 14 0 13 14 13 0 178 183 178 183 184 183 178 13 14 "

Now, if the user provides the string to be matched as '24578291' or 'BLUESOAPFrameworkV001' then it matches correctly
but if it includes characters (, ), [, ], / etc then it doesnt matches.

i'm matching the string as below -

Code:
$str =~ m/[latency][info] mpgw(BLUESOAPFramework/

The errors i receive are as below

Code:
Unmatched ( in regex; marked by <-- HERE in m/[latency][info] mpgw( <-- HERE BLUESOAPFramework/ at (eval 12)[/usr/lib/perl5/5.8.5/perl5db.pl:620] line 2.
at (eval 12)[/usr/lib/perl5/5.8.5/perl5db.pl:620] line 2
eval '($@, $!, $^E, $,, $/, $\\, $^W) = @saved;package main; $^D = $^D | $DB::db_stop;
$str =~ m/[latency][info] mpgw(BLUESOAPFramework/;
;' called at /usr/lib/perl5/5.8.5/perl5db.pl line 620
DB::eval called at /usr/lib/perl5/5.8.5/perl5db.pl line 3292
DB::DB called at -e line 1

Can someone please help?
Thanks in advance.

Moderator's Comments:
Mod Comment Please use code tags for listings and console output.
# 2  
Old 01-12-2011
You're using special characters like the parentheses, escape it to avoid the special meaning:

Code:
$str =~ m/[latency][info] mpgw\(BLUESOAPFramework/;

# 3  
Old 01-12-2011
But i can't skip it with escape character as the string to be matched is entered by the user and if it has special characters then the problem comes..i dont knw the user i/p in advance....let me know if i didnt understand wht you said.
# 4  
Old 01-12-2011
Ok , try something like this:

Code:
$str = "[latency][info] mpgw(BLUESOAPFramework): trans(24578291)[152.32.174.110]:Latency: 0 14 0 13 14 13 0 178 183 178 183 184 183 178 13 14 ";

$userInput = "[latency][info] mpgw(BLUESOAPFramework";

$userInput =~ s/(\(|\)|\[|\])/\\\1/g;

print "trans usrInput: $userInput\n";

if ( $str =~ m/$userInput/ ) {
   print $str."\n";
   }

# 5  
Old 01-12-2011
Thanks Klashxx Smilie
Tht worked perfectly. Thnx again.
# 6  
Old 01-12-2011
Another option would be to use quotemeta i.e.
Code:
my $userInput = "[latency][info] mpgw(BLUESOAPFramework";
$userInput = quotemeta($userInput);

print "trans usrInput: $userInput\n";
if ( $str =~ m/$userInput/ ) {
   print $str."\n";
}

These 2 Users Gave Thanks to fpmurphy For This Post:
# 7  
Old 01-12-2011
Quote:
Originally Posted by fpmurphy
Another option would be to use quotemeta i.e.
Code:
my $userInput = "[latency][info] mpgw(BLUESOAPFramework";
$userInput = quotemeta($userInput);

print "trans usrInput: $userInput\n";
if ( $str =~ m/$userInput/ ) {
   print $str."\n";
}

Nice one! thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with understanding this regex in a Perl script parsing a 'complex' string

Hi, I need some guidance with understanding this Perl script below. I am not the author of the script and the author has not leave any documentation. I supposed it is meant to be 'easy' if you're a Perl or regex guru. I am having problem understanding what regex to use :confused: The script does... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially... IN text file: {**************************** {test : test...test } Script: while (<$fh>) { push ( @data, $_); } if ( $data =~ m/(^{\*+$)/ ){ } With the above match i am... (5 Replies)
Discussion started by: avskrm
5 Replies

4. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

6. Shell Programming and Scripting

Perl REGEX - How do extract a string in a line?

Hi Guys, In the following line: cn=portal.090710.191533.428571000,cn=groups,dc=mp,dc=rj,dc=gov,dc=br I need to extract this string: portal.090710.191533.428571000 As you can see this string always will be bettween "cn=" and "," strings. Someone know one regular expression to... (4 Replies)
Discussion started by: maverick-ski
4 Replies

7. Shell Programming and Scripting

Perl Regex string opperation

I'm working on a basic log parser in perl. Input file looks like: len: 120713 foo bar file size of: testdir1/testdir1/testdir1/testdir1/testfile0 is 120713Of course there are tens of thousands of lines... I'm trying to compare the len and filesize values. #!/usr/bin/perl use strict; use... (2 Replies)
Discussion started by: dkozel
2 Replies

8. Shell Programming and Scripting

perl string matching problem (two backslash)

I have a few .tex files generated with html2latex. They have some extra \\ that generate error with pdflatex, so I would like to get rid of them. This perl -p -i -e s/\\\\//g myfile.tex with or without simple or double quote remove all of the backslashes, also the single ones needed by tex. How... (2 Replies)
Discussion started by: ahsog
2 Replies

9. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

10. Shell Programming and Scripting

string matching in perl

Hi, I have to search for a string in a variable. Say the variable is var1='ERROR: Make java] as enabled' here i want to match which are in red color. other like abc etc could change. Can you tell me the exact command something like the below in perl if ($var1=~ m/ERROR: Make... (3 Replies)
Discussion started by: ammu
3 Replies
Login or Register to Ask a Question