LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Pattern Matching Question (https://www.linuxquestions.org/questions/programming-9/perl-pattern-matching-question-357379/)

pete1234 08-26-2005 07:56 PM

Perl Pattern Matching Question
 
I'm fairly new to perl and I'm having a slight pattern matching issue.

The problem piece of code looks something like this:

until ($variable =~ /word/i) {
do things}

My problem is that as long as "word" is present the until function ignores whatever comes before word. I'd like to fix this.

I had the same problem with characters which came after "word", and I fixed that by doing something like this:

until (($variable =~ /word/i) and ($variable !~ /word./) and ($variable !~ /word./)){
do things}

This works fine for characters after "word", but I can't seem to get the check to work for characters that appear before "word".

Any help is appreciated.

spooon 08-27-2005 12:53 AM

what exactly are you trying to match?

if you want to match strings that say "word" exactly, then you can use /^word$/ (this use is unnecessary as "lc($variable) eq 'word' " will do). ^ asserts the beginning of the line, and $ asserts the end.

if you want to match strings where "word" is surrounded by whitespace or boundary of string, you can use /\bword\b/ (\b asserts word boundary)

you can also construct more complicated assertions

pete1234 08-27-2005 10:26 AM

Thank you very much for your help. /^word$/ was sufficient. I was trying to match different things, as I was having the problem with all my programs. Most irritating were a few programs I wrote for my girlfriend. One of which would check for our names, and she kept inserting obnoxiously things before my name and laughing. It had to be stopped. Thanks again.


All times are GMT -5. The time now is 08:10 PM.