Results 1 to 3 of 3

Thread: Regular expression in Perl

  1. #1
    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";
    }
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  3. #3
    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

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,642,359,302.77956 seconds with 17 queries