473,809 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in finding matching string

34 New Member
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 1469
c2eyes
5 New Member
As per my understanding you wana to grep first three string if it has naveen in it right.

Here is the solution :

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

~Sanjay
Jul 24 '07 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
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
2149
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
3542
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
3310
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 partial match could be any of the following:- Partial match 1: 1 character maybe wrong Partial match 2: 2 characters maybe wrong Partial match 3: 3 characters maybe wrong
7
5474
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
1292
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... The best way to explain this is to give an example of what the functions should do. * Let us a define a function as follows: Public Function Mask(ByRef inputStr As String, ByRef maskInStr As
8
2594
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: @"@?""""|@?"".*?(?!\\).""|''|'.*?(?!\\).'" I am inputting the entire source file string and using it with RegexOptions.Singleline. This works OK with, unless the string ends with a back-slash. For example: "This is a test\\". Can anybody see how to fix this...
14
13816
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*/ printf("%c\n",a);
2
1738
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 (the particular table structure is not relevant to this post). We need to identify the gender of the people as best as we can. We currently have two text files (MaleNames.txt and FemaleNames.txt) that list typically male or female names,...
11
4850
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 "?", where "*" matches zero or more consecutive occurrences of any character and "?" matches a single occurrence of any character. Does boost or some other library have this capability? If boost does have this, do i need to include an entire
1
10391
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
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 we have to send another system
3
3015
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.