Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Help with a Regex

by planetscape (Chancellor)
on May 12, 2005 at 10:18 UTC ( [id://456332]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Help with a Regex
in thread Help with a Regex

I believe I have finally vanquished the Demons of Derval Byslexia...

What I posted: _______________ I ......I IV .....V. V .....V. VI .....VI IX ....X.. X ....X.. XI ....X.I XIV ....XV. XV ....XV. XVI ....XVI XIX ....X.I X ....X.. XL ....X.. WRONG Should be: ...L... LX ...LX.. XC ....X.. WRONG Should be: ..C.... CLXIX ..CLX.I CDXLVI .D..XVI WRONG Should be: .D.L.VI MCMXCVI M.C.XVI MDCLI MDCL..I _______________

Here's more on what I want to do, and why:

1 2 3 4 5 6 7 Simple Rule: M D C L X V I

Input String: IV

Character: I
Desired position: 7
Next char: V (Desired position: 6)
I>V
7>6
Discard I; take V (Output: .......)

Character: V
Desired position: 6
Next char: NONE
V<NONE
Keep V (Output: .....V.)

End of Input; Output: .....V. (Agrees with Posted)


---

Input String: VI

Character: V
Desired position: 6
Next char: I (Desired position: 7)
V<I
6<7
Keep V; consider I (Output: .....V.)

Character: I
Desired position: 7
Next char: NONE
I<NONE
Keep I (Output: .....VI)

End of Input; Output: .....VI (Agrees with Posted)


---

 

IX ....X.. for the same reason IV .....V. XI ....X.I for the same reason VI .....VI



---

Input String: XIV

Character: X
Desired position: 5
Next char: I (Desired position: 7)
X<I
5<7
Keep X; consider I (Output: ....X..)

Character: I
Desired position: 7
Next char: V (Desired position: 6)
I>V
7>6
Discard I; consider V (Output: ....X..)

Character: V
Desired position: 6
Next char: NONE
V<NONE
Keep V (Output: ....XV.)

End of Input; Output: ....XV. (Agrees with Posted)


---

 

XIX ....X.I
(Special case because we already "saw" X - See MCMXCVI M.C.XVI for a complete discussion.)
CLXIX ..CLX.I
for the same reason XIX ....X.I


---

Input String: XL

Character: X
Desired position: 5
Next char: L (Desired position: 4)
X>L
5>4
Discard X; consider L (Output: .......)

Character: L
Desired position: 4
Next char: NONE
L<NONE
Keep L (Output: ...L...)

End of Input; Output: ...L... (DOES NOT Agree with Posted)


---

Input String: LX

Character: L
Desired position: 4
Next char: X (Desired position: 5)
L<X
4<5
Keep L; consider X (Output: ...L...)

Character: X
Desired position: 5
Next char: NONE
X<NONE
Keep X (Output: ...LX..)

End of Input; Output: ...LX.. (Agrees with Posted)


---

 

XC ..C.... for the same reason XL ...L... (DOES NOT Agree with + Posted)



---

Input String: CDXLVI

Character: C
Desired position: 3
Next char: D (Desired position: 2)
C>D
3>2
Discard C; consider D (Output: .......)

Character: D
Desired position: 2
Next char: X (Desired position: 5)
D<X
2<5
Keep D; consider X (Output: .D.....)

Character: X
Desired position: 5
Next char: L (Desired position: 4)
X>L
5>4
Discard X; consider L (Output: .D.....)

Character: L
Desired position: 4
Next char: V (Desired position: 6)
L<V
4<6
Keep L; consider V (Output: .D.L...)

Character: V
Desired position: 6
Next char: I (Desired position: 7)
V<I
6<7
Keep V; consider I (Output: .D.L.V.)

Character: I
Desired position: 7
Next char: NONE
I<NONE
Keep I (Output: .D.L.VI)

End of Input; Output: .D.L.VI (DOES NOT Agree with Posted)


---

Input String: MCMXCVI

Character: M
Desired position: 1
Next char: C (Desired position: 3)
M<C
1<3
Keep M; consider C (Output: M......)

Character: C
Desired position: 3
Next char: M (Desired position: 1)
C>M
3>1 BUT... we've already "seen" M...
Keep C; discard second M; consider X (Output: M.C....)

Character: X
Desired position: 5
Next char: C (Desired position: 3)
X>C
5>3 BUT... we've already "seen" C...
Keep X; discard second C; consider V (Output: M.C.X..)

Character: V
Desired position: 6
Next char: I (Desired position: 7)
V<I
6<7
Keep V; consider I (Output: M.C.XV.)

Character: I
Desired position: 7
Next char: NONE
I<NONE
Keep I (Output: M.C.XVI)

End of Input; Output: M.C.XVI (By some miracle, Agrees with Posted)


 

Complex Rule: Discard an earlier, one character match in favor of a longer, later match. In real data, earlier, one character matches also occur and should be discarded, because a longer match is probably on the way. Similarly, we probably want to get rid of characters we've already seen, unless... Unless those characters are SUPPOSED to also occur later, which is why I used the character classes in the regex in the code I posted... Arg.

Revised

Desired Output: _______________ I ......I IV .....V. V .....V. VI .....VI IX ....X.. X ....X.. XI ....X.I XIV ....XV. XV ....XV. XVI ....XVI XIX ....X.I X ....X.. XL ...L... LX ...LX.. XC ..C.... CLXIX ..CLX.I CDXLVI .D.L.VI MCMXCVI M.C.XVI MDCLI MDCL..I _______________

Now for me to figure out which monk-contributed code works with the revised output... I'm blind now, so...

Updated: Fixed two relatively minor typos.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://456332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found