Help needed with regular expressions

Jerry Davis jdawgaz at cox.net
Wed Jan 25 21:00:16 MST 2006


On Wednesday 25 January 2006 04:40 pm, John Wheat wrote:
>   Good Day,
>
>   I am seeking help to memorize, utilize, and gain basic proficiency
> with regular expressions and sed, grep, egrep searches and the like.
> When I read something like \^{a-r}\ my brain for lack of a bettwer way
> to say it believes this to be a ctl-d and stops all processing. So if
> you have a tool or an acronym or perhaps a silly song you have made up
> that would help me learn this aspect of CLI interaction would you please
> share that information with me.
>
> John Wheat

Here is a script I wrote many years ago, that helped me understand regexp's
I called testre

copy this to a file (what is between the ======= lines)
do a chmod u+x on the file

and do a testre -h
it will tell you what it needs

I did a quick regexp test on it by doing:

> testre '^[a-z][abc](.*)' "tcsh"
|tcsh|
captured value = sh

it will put |'s around what was actually matched.
and if you do capturing, i.e. a regexp inside of parens, it will show you what 
was captured.

Hope this helps!

Jerry

================== testre ====================
#!/usr/bin/env perl

# script: testre
# author: Jerry Davis
# date:   05/10/2001
#
#   This is a perl script which will show regular expression matches from 
various
#   inputs
#
#   The input can be from a file, or have any number of arguments.
#   testre -h will print help text of what is expected.
#
#   A very simplistic version of this was written by me, many years ago, when 
I was doing
#   major regular expression checking with PVCS output. I expanded this 
program specifically
#   for the MLUG presentation of REGEXP 101
#
#
#   testre:
#      number of args = 0, get both string and regex from file.
#      number of args = 1, expect string as redirected input, and regex = arg1
#      number of args = 2, arg1 = regex, arg2 = string

my ($filename, $string, $pattern);
$argnbr = @ARGV;  # get number of args

if (($argnbr == 1) && ($ARGV[0] eq '-h')) {
  print <<'  EOH';
  usage: testre (-h|[regex] [string]|[regex] < inputfile)
     number of args = 0, get both string and regex from file.
       in file there must be a [=== text ===] and [=== regex ===] tag to 
delimit
       what is the text to match, and the regular expression respectively
       for instance:
         [=== text ===]
         This is some text to match, but not at the end of the line
         [=== regex ===]
         (match)$

         note: the regexp is assumed to be on only one line

     number of args = 1, expect string as redirected input, and regex = arg1
     number of args = 2, arg1 = regex, arg2 = string
  EOH
  exit 0
}

# number of args = 2, arg1 = regex, arg2 = string
if ($argnbr == 2) {
  ($pattern, $string) = @ARGV; # get args into variables
  chomp ($string, $pattern);   # get rid of newlines
  showRE($string, $pattern);   # show the regexp
  exit;
}

# number of args = 1, expect string as redirected input, and regex = arg1
if (($argnbr == 1) && ($ARGV[0] ne '-h')) {
  $pattern = shift;   # get the pattern from arg1
  print "Input values:\n  pattern = $pattern\n\n";

  # now read all the lines from the file
  while ($string = <>) {
    chomp $string;
    showRE($string, $pattern);
  }
  exit;
}

# number of args = 0, get both string and regex from file.
if ($argnbr == 0) {
  my (@lines);  # lines array will hold all the lines of text

  while (<>) {
    chomp;
    if ($_ eq '[=== regex ===]') {
      $_ = <>;  # get next line as pattern
      chomp $_;
      $pattern = $_;
      print "Input values:\n  pattern = $pattern\n\n";
      #last;
    }

    next if ($_ eq '[=== text ===]');
    push @lines, $_;
  }

  foreach my $string (@lines) {
    showRE($string, $pattern);
  }
}

sub showRE {
  my ($s, $re) = @_;

#  return if $s = '';

  # in perl, $` is the before match
  #          $& is the actual match
  #          $' is the after  match

  if ($s =~ /$re/) {
    print "$`|$&|$'\ncaptured value = $1\n";
  }
#  else {
#    print "no match\n";
#  }
}
========================================









-- 
Hobbit Name: Pimpernel Loamsdown
Registered Linux User: 275424
 
This email's random fortune: Don't put off for tomorrow what you can do today 
because if 
you enjoy it today,
you can do it again tomorrow.


More information about the PLUG-discuss mailing list