Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: poolboi (Familiar Sight) on May 14th, 2008 07:00 AM
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

Code: ( text )
  1. /* 4 ABC:DEFG=12334556,BSERV=T22; */


i actualli used the regexp to match it

Code: ( text )
  1. foreach $data1 (@$data)
  2.     {
  3.       chomp ($data1);
  4.     if ($data1 =~ /4 \D\D\D:/){
  5.       print "$data1\n"; 
  6.       } }


but right how can i just get
Code: ( text )
  1. ABC:DEFG=12334556,BSERV=T22;


i tried using
Code: ( text )
  1. $data =~ /\D\D\D.*;/;


didn't work..so i'm not too sure about the syntax
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
nithinpes's Avatar
nithinpes
Expert
224 Posts
May 14th, 2008
09:03 AM
#2

Re: how to match front and end of a sentence using regexp?
Use:
Code: ( text )
  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.       } }

Reply
nithinpes's Avatar
nithinpes
Expert
224 Posts
May 14th, 2008
09:16 AM
#3

Re: how to match front and end of a sentence using regexp?
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'.

Reply
poolboi's Avatar
poolboi
Familiar Sight
168 Posts
May 15th, 2008
01:47 AM
#4

Re: how to match front and end of a sentence using regexp?
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?

Reply
nithinpes's Avatar
nithinpes
Expert
224 Posts
May 15th, 2008
04:11 AM
#5

Re: how to match front and end of a sentence using regexp?
Quote:
Originally Posted by poolboi
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,
Code: ( text )
  1. if ($data1 =~ /\/\* 4/) {

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:
Code: ( text )
  1. if ($data1 =~ /\Q/* @+/\E/) { # will match '/* @+/'
  2. if ($data1 =~ /\Q/* ...\E.+/) {# will match '/* ...' followed by one/more characters


I would suggest you to go through this:
Perl RE

-Nithin

Reply
poolboi's Avatar
poolboi
Familiar Sight
168 Posts
May 15th, 2008
05:52 AM
#6

Re: how to match front and end of a sentence using regexp?
alright
thanks you very much nithin
was looking for some reference too
thanks!
cheers, :)

Reply
poolboi's Avatar
poolboi
Familiar Sight
168 Posts
May 15th, 2008
10:00 AM
#7

Re: how to match front and end of a sentence using regexp?
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

Code: ( text )
  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. }}


and all these datas are added into MySQL database
i time it and ard 5-6 mins
u think it's already considered fast?

Reply
Reply
Not the answer you were looking for? Post your question . . .
174,853 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Perl Forum Contributors