Regular Expressions

Status
Not open for further replies.

torok

Ars Tribunus Militum
2,000
I'm having some trouble matching what I want to. I need to match:

data [--name--]data[-!name-!]

where the end "name data name" sequence can occure zero or one times as well as the begining data. This seem like it could be done as follows:

(.*?)(\[--(.+?)--\](.+?)\[-!\\3!-\])?

This doesn't work because since the first (.*?) will just stop after one letter since it doesn't need to be followed by anything. Does anyone know what I'm talking about? I don't know if I'm expressing it right. Anyone have any ideas on what I could do? Thanks.
 

sittidae

Ars Centurion
325
Subscriptor
What are you trying to match the data or the name? Your problem is that if either part is optional and you try to combine it with one RE then it will match an empty string. That is just like the FAQ's for finding a number in a string "an optional '-' sign followed by 0 or more digits an optional decimal point and 0 or more digits". Combine those together and you think that the following perl RE will do it:
code:<HR>
Code:
/-?\d*\.?\d*/
Of course since each part is optional it will match an empty string. So you need to resort to something like:
code:<HR>
Code:
/-?\d+\.?\d*/ || /-?\d*\.\d+/
Where you say that at least one side of the optional decimal point has to contain digits
 
Status
Not open for further replies.