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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expressions again

Status
Not open for further replies.

gammaman1

Programmer
Jun 30, 2005
23
US
you guys have all been a big help. I have one more question on regular expressions though. I want to make an if statement that will print out the line if a word is in the first pair of parenthesis. for example, these would print out:

1. (blah blah WORD) blah blah blah blah
2. (WORD) blah blah blah
3. blah blah blah (blah blah WORD) blah blah

but these would not:
1. (blah blah blah) blah blah (WORD)
2. (blah) (WORD)

this is what i have, but havn't gotten it to work. i'm pulling my hair trying to figure this out.

Code:
for ($i = 0; $i < @mylist; $i++)
{
	if (@mylist[$i] =~ /([^(WORD)]*)/)
	{	
		print "@mylist[$i]\n";
	}	
}
any hints or leads will be much appriciated.
thanks,
gammaman
 
Perhaps this (untested)?
Code:
if ( $mylist[ $i ] =~ /^[^(]*\([^)]*WORD[^)]*\)/ ) {
   # do something
}

Code:
if ( $mylist[ $i ] =~ /

 ^         # start of the string
 [^(]*     # any number of characters that aren't parenthesis (including none)
 \(        # followed by an opening parenthesis
 [^)]*     # followed by any number of characters that aren't closing parentheses
 WORD      # followed by the word you're looking for
 [^)]*     # followed by any number of characters that aren't closing parentheses
 \)        # followed by a closing parenthesis

/x ) {
As I said, it's untested so it may bomb miserably. At least you'll know what I was thinking at the time. You might want to add \bWORD\b instead of WORD, depending on your definition of what you want a word to be.

A small nitpick as well. When using a single element of an array, you should use $mylist[$i] rather than @mylist[$i].
 
this regex is probably the same as ish's (although i didn't copy... i promise!)

Code:
[b]#!/usr/bin/perl[/b]

@mylist = ( "(blah blah WORD) blah blah blah blah",
            "(WORD) blah blah blah",
            "blah blah blah (blah blah WORD) blah blah",
            "(blah blah blah) blah blah (WORD)",
            "(blah) (WORD)" );

foreach (@mylist) {
  if ( s/^([^(]*)[^)]*WORD[^)]*\).*$/$1/ ) {
    print "YES\n";
  } else {
    print "NO\n";
  }
}

outputs:-
YES
YES
YES
NO
NO


Kind Regards
Duncan
 
A different approach, without the regex overhead, is to just index for the necessary character positions.

Code:
my $word = 'WORD';
my $len = length($word);
foreach (@mylist) {
    print "$_\n" if index($_,'(') < index($_,$word)
                  and index($_,')') > (index($_,$word) + $len-1);
}

jaa
 
I like the index approach but it will need one slight modification. That will also print if there are no opening parenthesis characters in the string (i.e. it will return -1, which will be less than the index of $word).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top