473,396 Members | 1,996 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,396 software developers and data experts.

problem in finding matching string

34
Hi All,

I have the following strings seperated by space . I have to grep for Naveen having two values(Sha or See) in the string.

1)Naveen Sha reswww
2)Naveen See rex-www
3)Naveen See rex.x
4)Naveen xxxx cccccc
5)ABBCD See reggg
I have the following regexp.
$_ =~ /^Naveen[\s](Sha|See)[\s][.....]+$/

I want the first three strings above mentioned as output,

when I replace [.....] in regexp with [\d|\w], I am getting the first string as my output. but i need first three strings as an output.

Please help me regarding this.

Regards
Pnsreee
Jul 24 '07 #1
2 1425
c2eyes
5
As per my understanding you wana to grep first three string if it has naveen in it right.

Here is the solution :

$_=~/^(Naveen)\s+(Sha|She)\s+(\w+).+?/;

~Sanjay
Jul 24 '07 #2
numberwhun
3,509 Expert Mod 2GB
First, I would REALLY suggest that you go down to your local Barnes & Nobles store and pick up a copy of "Mastering Regular Expressions". That will greatly help you in better understanding regex's. The regex you supplied above is very messy and has a lot of mistakes. One right off the bat that stood out to me was your use of [ and ]. The square brackets typically dictate that the regex should match one of the characters held between them.

Now, taking your requirements, I came up with the following code that does what you wanted to do:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $file = 'naveen.txt';
  5. open(FILE, $file) or die "Can't open $file: $!";
  6.  
  7. while (<FILE>) {
  8.     chomp;
  9.     my $line = $_;
  10.  
  11.     if ($line =~ m/^Naveen\s+(?:Sha|See)\s+\w+[-.]*\w/) {
  12.         print "$line\n";
  13.     }
  14. }
  15.  
  16. close(FILE);
  17.  
I suggest you get the book and examine the regex above and learn to write cleaner, more exact regex's or you will have problems down the line in Perl. Just a suggestion.

Regards,

Jeff
Jul 24 '07 #3

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

Similar topics

4
by: aeuglein | last post by:
Hello! I have this RegEx: /(+:\/\/+)/i Now, I want to exlude on the end of a String the formats .gif / .jpg / ..png / .exe / .zip / .rar How I can this add to my regex ?
7
by: Niko | last post by:
Hi , I am writing script using multiple pattern matching. I need to replace the $_ =~ few times. Example: sub xx { open (TEMPF,"> $T_FILE");
2
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
10
by: exekutive | last post by:
I've been wrestling with this programming problem for some time now, so I thought I'd put it up for discussion. I'm trying to design a Mask function. See if you can figure out a code for it... ...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
2
by: Bob Johnson | last post by:
Using C#/2.0 I'm writing a small "data translator" utility app that reads data out of a MS Access database and inserts it into a SQL Server database. The source db lists a bunch of names of people...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.