regex

matching character

Page 1 of 1

1 Replies - 902 Views - Last Post: 17 November 2009 - 09:25 PM

#1 dmb168   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-November 09

regex

Posted 13 November 2009 - 08:22 AM

I'm having trouble figuring this out. I'm trying to write a regular expression that will match the first letter / character of my string and the last letter / character of the string.

Example: Ate data # it would match the A in Ate and the last a in data.
Time is it # it would match the T in Time and the t in "it."

This is what I have for my expression.

if ($string =~ m/(^a-z|a-z$)/)



That isn't correct at all.

I know the ^ is used to match the first part of the string and the $ sign is used to match the ending part of string.

I'm not quite sure on how to use them.


Here is my code:

#!/usr/bin/perl

while ()
{
print "enter string: \n";
chomp ($string =<STDIN>);

if ($string =~ m/(^a-z|a-z$)/)
{
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";
}
}



I did search on google for solution and from what I read the ^ searches the first part of the string and the $ searches the last part. But I need to find the double character.

Right now I'm not finding anything. That is one of my major error.

Is This A Good Question/Topic? 0
  • +

Replies To: regex

#2 numberwhun   User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 87
  • Joined: 28-March 08

Re: regex

Posted 17 November 2009 - 09:25 PM

View Postdmb168, on 13 Nov, 2009 - 07:22 AM, said:

I'm having trouble figuring this out. I'm trying to write a regular expression that will match the first letter / character of my string and the last letter / character of the string.

Example: Ate data # it would match the A in Ate and the last a in data.
Time is it # it would match the T in Time and the t in "it."

This is what I have for my expression.

if ($string =~ m/(^a-z|a-z$)/)



That isn't correct at all.

I know the ^ is used to match the first part of the string and the $ sign is used to match the ending part of string.

I'm not quite sure on how to use them.


Here is my code:

#!/usr/bin/perl

while ()
{
print "enter string: \n";
chomp ($string =<STDIN>);

if ($string =~ m/(^a-z|a-z$)/)
{
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";
}
}



I did search on google for solution and from what I read the ^ searches the first part of the string and the $ searches the last part. But I need to find the double character.

Right now I'm not finding anything. That is one of my major error.


Ok, I made some modifications to your script. The most significant is the use of the following pragmas:

use strict;
use warnings;



It is highly recommended that you use those as they will cut down on senseless syntax errors..

#!/usr/bin/perl

use strict;
use warnings;

my $string;

while ()
{
print "enter string: \n";
chomp ($string =<STDIN>);

#if ($string =~ m/(^a-z|a-z$)/)
if ($string =~ m/^([a-zA-Z]).*([a-zA-Z])$/)
{
  print "Found letter '$1' was found \n";
  print "Found letter '$2' was found \n";

  last;
}
else
{
  print "This string does not begin and end with the same character \n";
}
}




I left your original regex in there so you can see the differences. For your print statements about the first and last letters being the same... if I were you, I would test that instead of assuming.

Regards,

Jeff
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1