0

I wrote my first regex and an if condition to check a passed def argument loop. It doesn't seem to be working properly, and I am guessing I have something wrong with the regex. The condition runs, but it says the value doesn't match the regex when it does match the pattern I want, and vice-versa.

The pattern I need is like this: 123456_12345_ABC_123:

6 digits 
underscore
5 digits (may include 0 padding)
underscore
3 upper case letters
underscore
3 digits (may include 0 padding)

Here is my code:

if ev_val.match(/[0-9]{6}(_)[0-9]{5}(_)[a-z]{3}(_)[0-9]{3}/)

    puts "#{ev_val} matches required batch naming convention"
else
    puts "#{ev_val} doesn't match required batch naming convention\nSTOPPING SCRIPT!!!"
    exit
end

If anyone has any thoughts, I would greatly appreciate it!

0

1 Answer 1

2

Instead of:

/[0-9]{6}(_)[0-9]{5}(_)[a-z]{3}(_)[0-9]{3}/

You can use:

/\d{6}_\d{5}_[A-Z]{3}_\d{3}/
  • \d is a digit, AKA [0-9], so you should get used to using that short-hand.
  • (_) doesn't accomplish anything in your code, so don't capture the underscore, instead simply use _.
  • [a-z] captures all lower-case letters. You want upper-case so use [A-Z].
Sign up to request clarification or add additional context in comments.

4 Comments

Good but you should add 0*'s where he says may include 0 padding (unless I'm reading that wrong.)
Thank you for both of your responses! I had the \d first, but I had read it was exactly 1 digit, so that I thought that was why it wasn't working. I had also read parentheses looks for a literal character, which is why I put the underscore in them. Looks like I have much to learn for regex! I've been avoiding it, but I know that learning it will give me a great advantage in coding. Yes, any of the digits are 0 padded (all the way up to the first digit). I will try to look that up. but if someone knows the shorthand, I am much obliged.
Never mind. I figured it out! /0*\d{6}_0*\d{5}_[A-Z]{3}_0*\d{3}/ Thanks!
Just as a side note. This does find the string, even if there are characters on the end. It passed every test except 123456_12345_ABC_123zz That shouldn't pass, because of the zz, but it does. This is in a csv with double quotes around it. By the time the script is evaluating it, the double quotes are stripped, but I don't know why it allows the extra characters on the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.