RegEx Help Needed

Hey guys,

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.

Thanks,

Jon

The last group starts with a ? – remove it.
Remove the " at the end.

[quote=181169:@Eli Ott]The last group starts with a ? – remove it.
Remove the " at the end.[/quote]

I’m sorry - the " was a typo as I copied it over from my Xojo code.

Now, if I remove the ? mark as you suggest and use:

(.+)_(.+)_([0-9]+)?_([0-9]+)?_([0-9]+)?

Then only the first two strings are recognized.

My answer was wrong. Try:

(.+)_(.+)_?([0-9]+_)?([0-9]+_)?([0-9]+)

That matches but the subexpression groupings don’t turn out correct.

So I want the sub expressions to capture the fields I laid out. So in my original RegEx, the sub-expressions turn out as:

For Example 1: Button_TV1 Off_24_1_1 I then get:

In your RegEx these turn out as:

So that’s not correct either.

Super Kem, the Regex’ Guru of all times will come to your rescue ? :wink:

Where is Kem, when he is needed?

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…

([^_]+)_([^_]+)_(\\d+)_?(\\d+)?_?(\\d+)?

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

So this gets it then…

([^_\
]+)_([^_]+)_(\\d+)_?(\\d+)?_?(\\d+)?

Any reason you don’t simply use Split?

I could but it was more fun to try to figure out the RegEx and use it.

I’m trying to learn how regular expressions work and how to build them.

Believe me - I thought about using Split.