in reply to Re: old Perl regex problem
in thread old Perl regex problem

     I pasted your regex into my script exactly and it didn't work either :(. I found one that did, though.
     First, I want to make sure I understand yours. Please tell me if the following is correct...
/^PH(?:.{0,4}|.*(?![HI]-000).{5})$/

  1. starts with PH
  2. (?:)these parentheses aren't memory parentheses
  3. followed by 0 to 4 of any character or zero or more of any character
  4. (?!)return true if "H-000" or "I-000" would not match next
  5. followed by 5 of any character
  6. followed by the end of the line

     At first, I thought it failed because of the .{5} part. So, I changed it to {4,5} because the possibilities are "?H-000" (only 4 characters after the H) or "I?-000". That still didn't work. I was confused about why you used the (?:) and the quantifier and the or after the first wildcard dot, so I removed them. It still didn't work.

     After all that I re-started. I counted the characters between the PH and the part I wanted to check (\d{19}) and tried /^PH\d{19}[^I][^H]-000$/. It's a little easier because all of these files have a fixed format. That did work, but I wasn't sure I needed to quantify the characters between the PH and the end of the file name. So, I ended up with /^PH.*[^I][^H]-000$/ which works fine.

     I'm not sure why I couldn't get yours to work. I haven't used regex extensions or assertions before, so I want to understand them better.

Many thanks for your input.

Invulnerable. Unlimited XP. Unlimited Votes. I must be...
        GhodMode

Replies are listed 'Best First'.
Re: old Perl regex problem
by Abigail-II (Bishop) on Aug 05, 2002 at 09:59 UTC
    That will fail on file name ending in "IQ-000", as you demand that the sixth character from the end isn't equal to an I.

    You seem to be changing your requirements over time. This makes it hard to be helpful. As things stand now, I suggest:

    /^PH.*(?:[^I][^H]|I[^HF])-000$/
    Abigail