Trouble with rex - anchoring an expression to the end of the buffer

barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

I'm trying without success to build a rex expression. What I want is to match if my string contains a metamorph proximity operator (i.e., w/<n>) *at the very end of the string*.

I'm starting with this:

<rex "w/[1-9]=[0-9]*" $theString>

This does match if theString contains w/1, w/2, w/12, etc. But it also matches if theString contains w/1x, w/2foo, etc. Again, I want to constrain matches to occurances of "w/<some number>" AT THE VERY END OF THE BUFFER. I've tried a bunch of things (none correct, obviously), the last being:

<rex "[w/[1-9]=[0-9]*]=>>=" $theString>

Argh! Help, please. Thanks in advance.
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by mark »

<rex "w/=[0-9]+>>=" $theString>
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

But this also matches w/0 at the end of the buffer, which we don't want to allow. That's why I was starting with "w/[1-9]=[0-9]*"

I do want to match w/1, w/2, etc., but I don't want to match w/0. Ideally, I would also NOT want to match w/01, w/02, etc., but I can live with those.
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

"w/[1-9]=[0-9]*>>=" does not seem to work
User avatar
mark
Site Admin
Posts: 5519
Joined: Tue Apr 25, 2000 6:56 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by mark »

Sounds like an odd requirement (to ignore w/0). But since you're anchoring to the end the expression has to match right to left and [0-9]* eats up all the digits leaving nothing for [1-9] to match. You may need to use 2 expressions, one for single digits and another for multiple digits.
<$expr="w/[1-9]=>>=" "w/=[0-9]{2,}>>=">
<rex $expr $theString>
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

Thanks! That works great. Actually, using your suggestion to use two expressions as an inspiration (it never would have occured to me), the following works perfectly:

<$expr="w/[1-9]=>>=" "w/=[1-9][0-9]{1,}>>=">
<rex $expr $theString>

Yes, I agree that it seems like an odd requirement to ignore w/0! But the reason is that the app in which this code appears is taking ad hoc metamorph query strings and translating them for use in another, non-Texis database application. In the SQL of that other context, we want to prevent the logic suggested by w/0. The reason for wanting to prevent things like w/02 is purely esthetic. It just looks funky to me! :-)

Thank you again for your help, Mark.
User avatar
John
Site Admin
Posts: 2622
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Trouble with rex - anchoring an expression to the end of the buffer

Post by John »

Does ">>w/[1-9]=[0-9]*>>=" work?
John Turnbull
Thunderstone Software
User avatar
John
Site Admin
Posts: 2622
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Trouble with rex - anchoring an expression to the end of the buffer

Post by John »

Note that due to the way repetition operators work that w/=[1-9][0-9]{1,}>>= is probably not what you want, as the repetition applies to the "[1-9][0-9]" part, so it wouldn't match w/100, or anything with an odd number of digits.
John Turnbull
Thunderstone Software
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

You're right about my tweak of Mark's solution. It does NOT work for numbers with an odd number of digits >= 3.

But yes, the last suggestion absolutely does work! Thank you again. I'm looking forward to the day when the subtleties of regular expressions are clearly obvious to me. In the meantime, I'm greatful for the assistance this forum provides.
barry.marcus
Posts: 288
Joined: Thu Nov 16, 2006 1:05 pm

Trouble with rex - anchoring an expression to the end of the buffer

Post by barry.marcus »

OK. I'm not sure why I'm finding the logic of regular expressions so difficult to grasp, but I am... ARGH!!! Just dense, I guess. (Me or regular expressions?!?!) Now I find I need a similar expression, and once again I'm stumped.

This time I want to match if my string contains a "w/" proximity expression that is NOT well-formed, according to our application's definition of what-formed means. Specifically, any occurance of "w/" followed immediately by a sequence of one or more numerals, followed by a space, is well-formed, with the exception that "w/0" is not. So, in other words, I DO want to match ill-formed expressions such as "w/x", "w/2x", "w/0", "w/ 1", "w /1x", etc. I DO NOT want to match well-formed expressions such as "w/1 ", "w/12 ". (I can live with expressions such as "w/01", "w/022", etc. since our application will parse and convert "01" and "022" to 1 and 22, etc.)

Again, thank you all in advance. This forum is a very big help.

BTW, is there an in-depth explanation of regular expressions, other than the documentation for REX in the Vortex manual, to be found anywhere?
Post Reply