Wednesday, December 17, 2014

Learning Regex con't (xxxxxxx-xxxx where x's are digits)

This one (regex) didn't take me as long to figure out given all that I read about regular expressions.

As part of my homework for Learning Ruby the Hard Way, I'm still working on my editor that takes 8 pieces of information and inserts it all into a tab delimited format .txt file.  The second column needs to be in the format of xxxxxxx-xxxx where the x's are digits and the hyphen needs to be included.  This is an account id in the system I'm working with where I'm going to eventually create a script to combine rows from files a 3rd party agency is giving to us.

Back to the editor, I created a regex to only accept an account id that has 7 digits, a hyphen followed by 4 more digits.  This one was easy compared to the online tutorials I was reading over.  Just inserting the regex here so I'll have it for future reference.

/^\d{7}\-\d{4}$/

Maybe by the time I'm finished with the 2 projects I want to complete, I'll have a cook book of regex's.

No comments:

Post a Comment