Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA

Hi

I'm trying to partially match some strings.

Example:

Code:
livvy lump-beach
orangesty

I'm looking to match, in the first case:
livvy, lump-beach, lump, beach or the entire string [livvy lump-beach[/b].
For the second string, I would only match the entire string: orangesty

To do so, I'm currently using the following.
Code:
$string_1 =~ /\B$string_to_match\B/i;

Even If I use the \B, to match non word boundary, it's doesn't match the string patially.

 
I think you mean \b (word boundary), rather than \B - it's case-sensitive.
 
I'm not sure what you're looking for then. Can you post the rest of your code please (esp. where $string_to_match and $string_1 are set).
 
Hi extension,

I suppose you're looking for something like this?

Code:
#!/usr/bin/perl -w
use strict;

my @patterns = ();
my %seen = ();

# We create a list of all patterns we wish to find

foreach my $expression ("livvy lump-beach", "orangesty") {
  foreach my $part (split(/\s+/, $expression)) {
    push @patterns, qr/\b$part\b/ unless $seen{$part}++;
  }
  foreach my $part (split(/\W+/, $expression)) {
    push @patterns, qr/\b$part\b/ unless $seen{$part}++;
  }
  push @patterns, qr/\b$expression\b/ unless $seen{$expression}++;
}

# We prepare the text

my $text = "foo bar livvy lump-beach foo orangesty";
study($text);

# We match all the patterns against the text and print the results

foreach my $pattern (@patterns) {
  while ($text =~ /($pattern)/g) {
    print $1, "\n";
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top