By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

how to match front and end of a sentence using regexp?

100+
P: 170
hi, this is somewhat link to the other post i posted
but i shall put it in another topic for easy reference

previously i had

Expand|Select|Wrap|Line Numbers
  1. /* 4 ABC:DEFG=12334556,BSERV=T22; */
  2.  
i actualli used the regexp to match it

Expand|Select|Wrap|Line Numbers
  1.     foreach $data1 (@$data)
  2.     {
  3.       chomp ($data1); 
  4.     if ($data1 =~ /4 \D\D\D:/){
  5.       print "$data1\n";     
  6.       } }
  7.  
but right how can i just get
Expand|Select|Wrap|Line Numbers
  1.  
  2. ABC:DEFG=12334556,BSERV=T22;
  3.  
i tried using
Expand|Select|Wrap|Line Numbers
  1. $data =~ /\D\D\D.*;/;
  2.  
didn't work..so i'm not too sure about the syntax
May 14 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
Use:
Expand|Select|Wrap|Line Numbers
  1. foreach $data1 (@$data)
  2.     {
  3.       chomp ($data1); 
  4.     if ($data1 =~ /4 (\D\D\D:.+)\*/){
  5.       print "$1\n";  # $1 will hold the pattern inside parantheses
  6.       } }
  7.  
May 14 '08 #2

nithinpes
Expert 100+
P: 410
The title of your post doesn't match your description. If you actually wanted to match begining and end of a line, here is how to do it.
The symbol '^' will denote beginning of line/string inside pattern match. For ex:
/^The/ will match only the lines begining with 'The'.

Similarly, the symbol '$' will denote end of line/string inside pattern match. For ex:
/end$/ will match only the lines ending with 'end'.
May 14 '08 #3

100+
P: 170
hm...thanks nithinpes

i might need the code for beginning and end of statements

i guess for this case it should somehow be match a part of a sentence ending with special characters like "*", "%", and stuff

anyway if i use ".*" it matches 1 character 0 or more times right?
and ".+" it matches 1 character 1 or more times right?
May 15 '08 #4

nithinpes
Expert 100+
P: 410
hm...thanks nithinpes

i might need the code for beginning and end of statements

i guess for this case it should somehow be match a part of a sentence ending with special characters like "*", "%", and stuff

anyway if i use ".*" it matches 1 character 0 or more times right?
and ".+" it matches 1 character 1 or more times right?
Yes. That's right!
One way of matching special characters is to escape them(using '\'). For example in your text, if you wanted to match lines with '/* 4' you could use,
Expand|Select|Wrap|Line Numbers
  1. if ($data1 =~ /\/\* 4/) { 
  2.  
Also, you could use \Q and \E. \Q will remove any special meaning, inside pattern match, of the characters that follow it upto \E.
For ex:
Expand|Select|Wrap|Line Numbers
  1. if ($data1 =~ /\Q/* @+/\E/) { # will match '/* @+/'
  2. if ($data1 =~ /\Q/* ...\E.+/) {# will match '/* ...' followed by one/more characters
  3.  
I would suggest you to go through this:
Perl RE

-Nithin
May 15 '08 #5

100+
P: 170
alright
thanks you very much nithin
was looking for some reference too
thanks!
cheers, :)
May 15 '08 #6

100+
P: 170
hey guys another question to ponder about

how long u think it takes to do a search on my code below
roughly around 2,375 KB of data

Expand|Select|Wrap|Line Numbers
  1. foreach $data1 (@data)
  2. {
  3. chomp ($data1); 
  4.  
  5. if ($data1 =~ /3 SESSION/ && $data1 !~ /2 TBHLR/ && $data1 !~ /1 TBHLR/ && $data1 !~ /4c COMMAND/ && $data1 !~ /4c 
  6. SESSION/){
  7.  
  8. my $user_line = index ($data1, "3 SESSION");
  9. my $user = substr ($data1, ($user_line+23),6);
  10. push @array1,$user;
  11.  
  12. my $time_line = index ($data1, "USERID");
  13. my $time = substr ($data1, ($time_line+25),8); 
  14. push @array2,"$time\n";
  15.  
  16. push @array3, "$date\n";
  17.  
  18. }elsif
  19. ($data1 =~ /4 (\D\D\D:.+)\*/){
  20. push @array4, "$1\n";
  21.  
  22. }}
  23.  
and all these datas are added into MySQL database
i time it and ard 5-6 mins
u think it's already considered fast?
May 15 '08 #7

Post your reply

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