I’m trying to develop a protocol/API that gets passed into an object. Trying to use a RegEx to handle the request that’s passed in. So the request is of a form like this:
Type_Name_######
are numerical fields. The second and third numerical fields are optional. So here are some actual examples of data being passed in:
So see the pattern?
Now, I have the following RegEx set up:
(.+)_(.+)_([0-9]+)?_([0-9]+)?_?([0-9]+)?"
It works fine for the first three examples, but does not return a match for the last entry.
I’ve tried different optional combinations and just can’t get one to work. Obviously this must be possible but my RegEx skill are still in their infancy.
Yeah, I know. He’ll have some brilliant code that will be so simple.
The confusing issue and why Eli’s Reg-Ex didn’t work right is that the RegEx engine evaluates starting at the END of the string and working backwards and not the beginning and working forwards. So I’m not working backwards correctly.
See, since there’s repetition in the input string pattern I’ve also tried:
(.+)_(.+)(_([0-9]+)){1,3}
And that matches everything. But the sub-expressions still are not correct. I know the .+ fields are kinda screwing it up a bit but those characters can be anything…
That works. The ONLY thing that I don’t like is that if I have a bunch of commands in multiple lines, it counts the new line character as the first character in the match.
That’s likely not going to happen in my code but the super perfectionist side of me is seeing this and bothered by it! :o