Re: perl look ahead regular expression that is optional?
by Corion (Patriarch) on Oct 03, 2013 at 17:06 UTC
|
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: perl look ahead regular expression that is optional?
by talexb (Chancellor) on Oct 03, 2013 at 20:37 UTC
|
This is an interesting question -- I like brain-teasers as much as the next developer, so I wrote a solution. Except it doesn't work, because I think the example you have is faulty. Perhaps someone else can find the flaw in my reasoning.
Find a string 'foobar' that is followed by two sets of optional
characters
- the first set of characters might contain a b or c,
in that order. Only zero or one a, zero or one b, and zero or one c is allowed.
- The second set of characters is always preceded by an X (to delimit
that this is the second set) and might contain a b or c,
Only zero or one a, zero or one b, and zero or one c is allowed in the second set.
OK -- foobar, then optionally (zero or one 'a', 'b', 'c'), then optionally ('X' followed by zero or one 'a', 'b', 'c'). But then there's a list of examples, and #5 seems to break the rule about the second group ..
foobarabcX must not match, nothing follows 'X'
I'm not sure this is right. The second optional group has been defined as 'X', followed by zero or one 'a', 'b' and 'c'. Which means I could have an 'X' followed by zero 'a', 'b' and 'c'. Which means that foobarabcX is a valid pattern.
Anyone else see the flaw?
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
| [reply] [d/l] [select] |
|
|
Management comes up with these contradictory requirements all the time :)
But, really, this just looks like they mean that there must be SOMETHING after the X, but it could be a, b, OR c, or more than one of those, in relative order.
So, Xa is ok, so is Xb, Xc, Xab, Xac, Xbc, and Xabc. Just X by itself is invalid - it's not allowed to be empty.
I seem to recall such types of ambiguity to be common both among teachers and managers. Something about not having to implement it themselves combined with "I know what I mean. Why don't you?".
And, based on my tests, should do the job. But if the OP doesn't understand why while trying to hand it in as their own work, they'll find that they're going to get further and further behind as their course progresses.
| [reply] [d/l] |
|
|
| [reply] |
|
|
Thanks for posting your solution to this -- I've managed OK with regexps, but your post encouraged me to get a little more familiar with how lookaround assertions work. I'm going to document the regexp you've provided in order to show that I believe the instructions are still incorrect (see also my post here where I list all of the possible legal patterns that I think exist).
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
| [reply] [d/l] |
|
|
|
|
|
|
Awesome, it works! 13 years programming Perl, and I'm still learning new sh*t.
| [reply] |
Re: perl look ahead regular expression that is optional?
by Skeeve (Parson) on Oct 03, 2013 at 21:11 UTC
|
Do you see where this contradicts?
compare: The second set of characters is always preceded by an X (...) and might contain (...) Only zero or one a, zero or one b, and zero or one c is (...)
to: foobarabcX must not match, nothing follows 'X'
I think it must match as X is followed by exactly zero "a"s, "b"s and "c"s.
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Yes, you are correct - as written it could be interpreted to mean that zero or more characters after the 'X' are allowed.
My write up of the problem was in error.
If there are ANY flags in the second set of characters, there is a 'X' preceding them.
It is illegal to have an 'X' without a following character.
The basics are correct, though. I have two optional sets of flags (single characters) that I need
to parse out into separate variables.
By the way... have tried to simplify the problem. I am trying to parse a much more complicated string set.
| [reply] |
Re: perl look ahead regular expression that is optional?
by mtmcc (Hermit) on Oct 03, 2013 at 20:50 UTC
|
| [reply] |
Re: perl look ahead regular expression that is optional?
by talexb (Chancellor) on Oct 04, 2013 at 14:51 UTC
|
Well, this post has been fascinating to work on. I was starting to question my ability to comprehend the English language (my mother tongue), and this morning re-read your post, including the part that said
When the second set is present, it is always prececeded by an 'X':
That's it, I thought -- but then I re-read the part above that says
The second set of characters is always preceded by an X (to delimit
that this is the second set) and might contain a b or c,
Only zero or one a, zero or one b, and zero or one c is allowed in the second set.
So the second set *always* starts with an 'X', and *might* contain a, b or c. How can I prove this? By writing a script to grind through all of the permutations, of course.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
| [reply] [d/l] [select] |