Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-11-2009
Member
 
Join Date: Jul 2009
Posts: 140
Regular expression in Perl

Hi
I am trying to write a regular expression in perl that will match the first letter / character of a string and the last letter / character of the string. I have this statement for my expression.
Code:
if ($string =~ m/(^([a-z]+\s*)+$)/)
I think this is not correct, because I know that the ^ is used to match the first part of the string while the $ sign is used to match the ending part of string. i am not sure about my code, still I am posting my code, please help me.
Code:
#!/usr/bin/perl
while ()
{
print "enter something: \n";
chomp ($string =<STDIN>);
if ($string =~ m/(^([a-z]+\s*)+$)/)
{
print "Found letter '$1' was found ", pos($text), " \n";
print "This string begins and ends with same character with \n";
last;
}
else
{
print "This string does not begin and end with the same character \n";
}
}
Reply With Quote
  #2  
Old 19-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Regular expression in Perl

Hello
Are you trying to test a given string starts and ends with the same character? I just want to be sure that I have understood your problem correctly. If this is the case, I have tried it in two way. Find out what the first letter is and insert it into a variable, then find out what the last letter is and put it into another variable. And then you have to compare the tow strings. In this case there is no need of a regular expression. In general you have to first extract the first and the last letters. then construct your regular expression dynamically. and finally do the match.
Reply With Quote
  #3  
Old 19-11-2009
Member
 
Join Date: Jul 2009
Posts: 140
Re: Regular expression in Perl

Hi
Thanks for replying
I have used the following statement in my code.
Code:
if ($string =~ m/^(.+)+(.+)\1$/)
1) axxxxxbxxxxxa
This string begins and ends with same character with
2) If the end is near
This string does not begin and end with the same character
The problem is that it will only check one way, I need it to check both ways. Do you have any suggestions
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Regular expression in Perl"
Thread Thread Starter Forum Replies Last Post
Using Regular Expression than Substring in Microsoft Excel Jigisha MS Office Support 5 25-01-2012 06:19 PM
Regular expression Help in asp.net for DOB. maheshmartha Software Development 2 06-03-2010 07:06 AM
Regular expression in MySQL Fragman Software Development 4 02-03-2010 10:15 PM
why to use * at the start of a C expression? Nadiaa Software Development 4 10-02-2010 10:57 PM
Convert php expression SoftWore Software Development 3 05-12-2009 02:21 PM


All times are GMT +5.5. The time now is 08:32 AM.