Re: Checking last character of string for punctuation

From: Philipp Traeder (philipp_at_hitchhackers.net)
Date: 02/03/05


To: beginners@perl.org
Date: Wed, 2 Feb 2005 23:50:59 +0000

On Wednesday 02 February 2005 22:39, Chris Schults wrote:
> Howdy all. I'm a newbie with a newbie question. I assume that I'm in the
> right place.

Yes, thie does sound consistent. ;-)

> I need to update some code a developer programmed for my organization.
>
> The purpose of the code is really basic. It simply prints: $title.
> $subtitle.
>
> However, some of my titles and subtitles end with punctuation ("?", "!",
> ".", "..."). Thus, I end up with: Am I a title that ends with punctuation?.
> Do-oh!.
>
> So, I need to revise the code so that it first checks each string to see if
> it ends with a punctuation character. If it does, don't add the period. If
> not, do add the period. Note: the string should be kept as is.
>
> I believe the key is a regular expression of some sort, but I can't seem to
> figure it out.

You're definitely on the right track using regular expressions - for starters,
you might want to take a look at

  perldoc perlre
  perldoc perlretut

You probably want a construction like

  if ($variable_that_might_end_in_punctuation !~ /<regular expression>/) {
    # add the period
  }

This matches your string against the regular expression that you need to put
between the slashes (without the less-than/greater-than characters) and
returns true if the string does *not* match the regular expression (the
opposite operator would be
  $variable =~ /<regexp>/
which returns true if the regexp matches.

Given the description of your problem, you might be interested in character
classes - they are described in perlretut (section "Using character
classes").

HTH,

Philipp



Relevant Pages

  • Small regular expression parser
    ... the goal was to develop a very simple regular expression parser. ... sets are selected using the % character instead of \. ... into the string of the start of the match and the length of the match. ... Last there are a couple macros to help with captures. ...
    (comp.lang.lisp)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... abandoned it because I don't think Beautiful Code can be written in C: ... Pike's code doesn't implement a regular expression interpreter ... it makes no provision for a character which must occur at ... changeable index into the string it points-at and it expects the user ...
    (comp.programming)
  • Re: RegularExpressions
    ... Start at the beginning of the string, or at the first line break character ... Basically, that is what a regular expression does, but in a more roundabout ... Notice that before the word BRE there are some other info that I dont ...
    (microsoft.public.dotnet.framework)
  • Re: MT NewsWatcher filtering
    ... Jim Gibson wrote: ... The plus sign shouldn't be necessary for most regular expression ... character 1-6 exists in the string. ... exists in the string you are testing. ...
    (comp.sys.mac.system)
  • RE: Checking last character of string for punctuation
    ... Checking last character of string for punctuation ... I'm a newbie with a newbie question. ... Am I a title that ends with punctuation?. ...
    (perl.beginners)