Created
December 11, 2013 10:38
-
-
Save ichistmeinname/7908294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#natRegexp = /(0|[1-9]([0-9])*)/; | |
# Definition einer FUNKTION(!) für natRegexp | |
# Matche natRegexp gegen `str` an Position `pos` | |
def match_0O1Bis90Bis9S(pos,str) | |
# Es kann folgende Unterscheidung für den nächsten Buchstaben gemacht werden: | |
# Matche /0/ gegen `str` an Position `pos` | |
p1 = match_0(pos,str); | |
# Matche /[1-9][0-9]*/ gegen `str` an Position `pos` | |
p2 = match_1Bis90Bis9S(pos,str); | |
# Gib den längeren Match zurück | |
return max(p1,p2); | |
end; | |
# Matche /0/ gegen `str` an Position `pos` | |
def match_0(pos, str) | |
if str[pos,1] == "0" then | |
# Matche // gegen `str` an Position `pos+1` | |
return match_(pos+1,str); | |
else | |
# Es wurde kein Matching gefunden, gib `nil` zurück | |
return nil; | |
end; | |
end; | |
# Matche // gegen `str` an Position `pos` | |
def match_(pos,str) | |
# ist immer erfolgreich und gibt die Position zurück | |
return pos; | |
end; | |
def match_1Bis90Bis9S(pos, str) | |
# definiere lokale Variable, da wir den Wert gleich zweimal verwenden | |
strPos = str[pos,1]; | |
# Matche /[1-9]/ gegen `str` an Position `pos` | |
# wir können hier einfach die Strings vergleichen | |
if strPos >= "1" && strPos <= "9" then | |
# Matche /[0-9] gegen `str` an Position `pos+1` | |
return match_0Bis9S(pos+1, str); | |
else | |
# Es wurde kein Matching gefunden, gib `nil` zurück | |
return nil; | |
end; | |
end; | |
# Matche /[0-9]*/ gegen `str` an Position `pos` | |
def match_0Bis9S(pos,str) | |
# Es kann folgende Unterscheidung für den nächsten Buchstaben gemacht werden: | |
# Matche // gegen `str` an Position `pos` | |
p1 = match_(pos, str); | |
# Matche /[0-9][0-9]*/ gegen `str` an Position `pos` | |
p2 = match_0Bis90Bis9S(pos,str); | |
# Gib das längere Matching zurück | |
return max(p1,p2); | |
end; | |
# Matche /[0-9][0-9]*/ gegen `str` an Position `pos` | |
def match_0Bis90Bis9S(pos,str) | |
# definiere lokale Variable, da wir den Wert gleich zweimal verwenden | |
strPos = str[pos,1]; | |
# Matche /[0-9]/ gegen `str` an Position `pos` | |
# Wir können hier einfach Strings vergleichen | |
if strPos >= "0" && strPos <= "9" then | |
# Matche /[0-9] gegen `str` an Position `pos+1` | |
return match_0Bis9S(pos+1, str); | |
else | |
# Es wurde kein Matching gefunden, gib `nil` zurück | |
return nil; | |
end; | |
end; | |
def startMatch(str) | |
# Starte an Position 0 | |
p = 0 ; | |
p1 = match_0O1Bis90Bis9S(p, str); | |
# Suche Schritt für Schritt nach einem Matching | |
while p1 == nil && p < str.length do | |
# solange nichts gefunden wird | |
# und wir den String noch nicht abgearbeitet haben | |
p = p + 1; | |
p1 = match_0O1Bis90Bis9S(p, str); | |
end; | |
if p1 == nil then | |
# Kein Matching gefunden, gib nil zurück | |
return nil; | |
else | |
puts("Position: " + p.to_s); | |
puts("Letzte Position: " + p1.to_s); | |
# Gib das Ergebnis zurück | |
return str[p, p1-p]; | |
end; | |
end; | |
# Hilfsfunktion: Gib das Maximum der Positionen p1 und p2 zurück | |
def max(p1, p2) | |
if p1 == nil then | |
return p2; | |
else | |
if p2 == nil then | |
return p1; | |
else | |
return (if p1 < p2 then p2 else p1 end); | |
end; | |
end; | |
end; | |
puts("Gematchter String: " + startMatch("ab1337ef")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment