Autor Beitrag
Green
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: So 14.05.06 09:42 
also ich hab eine PHP funktion welche ich gerne umschreiben würde in Pascal, aber ich weiss irgendwie nicht so recht was ich mit den RegEx funktionen machen soll und wie die entsprechungen in Delphi heissen...

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
  function parse_message($str = '') {
    if (preg_match('#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#', trim($str), $message)) {
      if (preg_match('#^(.*)!(.*)@(.*)#', $message[1], $host)) {
        $msg['nick']   = $host[1];
        $msg['ident']  = $host[2];
        $msg['host']   = $host[3];
        $msg['server'] = '';
      } else {
        $msg['nick']   = '';
        $msg['ident']  = '';
        $msg['host']   = '';
        $msg['server'] = $message[1];
      }

      $msg['type']  = $message[2];
      $msg['param'] = preg_split('#\s+#', trim($message[3]));
      $msg['text']  = (empty($message[4]) ? '' : trim($message[4]));
    }

    return !empty($msg) ? $msg : false;
  }


ich hab auch schon n bissel angefangen ins delphi zu 'übersetzen' :
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
{
msg[0] = nick
msg[1] = identd
msg[2] = host
msg[3] = server
msg[4] = type
msg[5] = param
msg[6] = text
}

function parse_message(str = '') then begin
    if (preg_match('#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#', trim(str), message)) then begin
      if (preg_match('#^(.*)!(.*)@(.*)#', message[1], host)) then begin
        msg[0]  := host[1];
        msg[1]  := host[2];
        msg[2]  := host[3];
        msg[3]  := '';
      end else begin
        msg[0]  := '';
        msg[1]  := '';
        msg[2]  := '';
        msg[3]  := message[1];
      end;

      msg['type']  := message[2];
      msg['param'] := preg_split('#\s+#', trim(message[3]));
      msg['text']  := (empty(message[4]) ? '' : trim(message[4]));
    end;

    return !empty(msg) ? msg : false;
  end;


ich hoffe es kann mir jemand helfen, besonders bei den letzten zeilen und den RegEx

mfG Green
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 14.05.06 10:48 
Für die Regulären Ausdrücke brauchst Du Zusatzkomponenten, z.B. Suche in: Delphi-Forum TREGEX \ Suche in: Delphi-Forum TREGEXPR.

Für die Konstruktionen mit Trinär-Operator:

Entweder mit If Then Else übersetzen oder ab D7 mit der Funktion IfThen ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: So 14.05.06 12:55 
äähm, ja ...
was meinst du mit Trinär Operator? und wie würde das konkret aussehen?

und wie genau heissen die eintsprechenden funktionen in delphi zu preg_match preg_split und empty ? trim gibts ja glaub ich...

mfG Green
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 14.05.06 13:07 
user profile iconGreen hat folgendes geschrieben:
äähm, ja ...
was meinst du mit Trinär Operator? und wie würde das konkret aussehen?


Trinär ist dieser hier : Bedingung ? True : False; Sieht in Delphi dann (ab D7) so auf: IfThen(Bedingung, True, False);

user profile iconGreen hat folgendes geschrieben:
und wie genau heissen die eintsprechenden funktionen in delphi zu preg_match preg_split und empty ? trim gibts ja glaub ich...

mfG Green
Bitte lerne lesen, ich habe bereits oben erwähnt, dass Du für die preg_matches ne Zusatz-Kompoennte brauchst ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: So 14.05.06 13:36 
ich hab mir die TregExpr kompo runtergeladen, (besser gesagt die unit ^^) aber ich versteh halt nicht was jetzt die entsprechung zu preg_match ist...
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: So 14.05.06 13:41 
und nenn die variable nicht message, das wort ist reserviert
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 14.05.06 13:47 
Für Beispiele zur Nutzung siehe bitte regexpstudio.com/DE/...Expr/Help/demos.html
Dort sind die Dinge, die Du hier im Source brauchst erklärt ...

Achso: In deinem Delphi-Source fehlen die Typ-Angaben der Parameter. Diese müsstest Du noch mit ergänzen, sonst funzt das nicht ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: So 14.05.06 14:03 
user profile iconBenBE hat folgendes geschrieben:
Für Beispiele zur Nutzung siehe bitte regexpstudio.com/DE/...Expr/Help/demos.html
Dort sind die Dinge, die Du hier im Source brauchst erklärt ...


jo, ich guck se mir mal an...

user profile iconBenBE hat folgendes geschrieben:

Achso: In deinem Delphi-Source fehlen die Typ-Angaben der Parameter. Diese müsstest Du noch mit ergänzen, sonst funzt das nicht ...


schon klar, ich hab auch nicht mehr gemacht ausser die $ gelöscht ^^

mfG Green
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Mo 15.05.06 14:35 
ey halt mich für blöd, aber die hilfe versteh ich ÜBERHAUPT nicht... was muss ich denn jetzt machen wenn ich sowas wie preg_match haben will? ExexRegExpr gibt ja nur true/false zurück, ich brauch aber das was bei (.*) steht
wie mach ich das denn jetzt??
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Mo 15.05.06 17:51 
die forensuche benutzen, da steht ne menge drin ;)
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Mo 15.05.06 18:18 
Kleiner Tipp. Wenn du die Arrays mit Strings als Index nicht ganz missen willst, das geht in Pascal fast genauso. Mach dir einfach eine ähnliche Funktion:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
function iMyOrd (sStr : String): word;
var 
  saNeeded : Array[0..6] {halt die Dimensionen des Arrays} of String = 
    ('nick', 'identd', 'host', 'server', 'type', 'param', 'text'); {halt die Indizes in der richtigen Reihenfolge!!}
  i : word;
begin
  Result := -1; {Fehlerrückgabe}
  for i := 0 to high(saNeeded) do
    if saNeeded[i] = sStr then Result := i;
end;

So, jetzt kannst du deine String-indizierten Arrays fast wieder verwenden:
ausblenden Delphi-Quelltext
1:
msg[iMyOrd('nick')] := blabla;					

Idee verstanden? Gut, wenn ja, wenn nein: die Funktion gibt den Integerwert von deinem String-Index zurück. Ein Feature, was PHP hat, kannst du hier nur bedingt einsetzen: Ein Array kann in PHP alle möglichen Datentypen beinhalten. Das geht bei Delphi nur mit Variant-typen, und die können auch nur einfache Typen übernehmen. So mit Array und so is nicht.
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 15.05.06 18:51 
@nullplan: Für Hashes (Assoc. Arr.) kann man aber auch TStringList.Values[]) verwenden. Nur so nebenbei ...

Ich denk mal, wo's noch hängt, ist die Nutzung der TRegExpr-Kompo. Da ich aber ihm schon einen direkten Link mit zahlreichen Beispielen und Anwendungsbeispielen gegeben habe (wo maan fast nur noch abschreiben muss), möcht ich dazu vorerst nichts weiter sagen. Ich meld mich hier wieder, wenn ich seh, dass zumindest ein Versuch zum Einsetzen der Sources in die Beispiele unternommen wurde.

Evtl. hilfreich sollte auch crashkurs.christian-stelzmann.de/ sein ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Di 16.05.06 19:37 
also ich hab ma was angefangen, häng jetzt aber an preg_split ...

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
function TForm1.PregMatch (const AInputString,RegEx : string) : TStringDynArray;
var
   r : TRegExpr;
   i : integer;
begin
   //Result := '';
   SetLength(Result,0);
   r := TRegExpr.Create; { Erzeuge Objekt }
   try { garantiere Speicherfreigabe }
      r.Expression := RegEx;
       { der R.A. wird automatisch in die interne Struktur übersetzt
         innerhalb der Zuweisung an diese Eigenschaft}

      if r.Exec (AInputString) then
         REPEAT
            for i:=0 to high(i) do
               Result[i] := r.Match [i];
         UNTIL not r.ExecNext;
      finally r.Free;
   end;
end;

function TForm1.ParseMessage(const AParseData : string) : TStringDynArray;
  var matches,host : TStringDynArray;
begin
  SetLength(matches,0);
  SetLength(matches,0);
  SetLength(Result,0);
  if ExecRegExpr('#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#', AParseData) then begin
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if ExecRegExpr('#^(.*)!(.*)@(.*)#', matches[1]) then begin
      host := PregMatch(AParseData , '#^(.*)!(.*)@(.*)#');
        Result[0] := host [1];
        Result[1] := host [1];
        Result[2] := host [1];
        Result[3] := host [1];
    end else begin
        Result[0]  := '';
        Result[1]  := '';
        Result[2]  := '';
        Result[3]  := matches[1];
    end;
    Result[4] := matches[1];
    SplitRegExpr('#\s+#' , Trim(matches[3]) , Result[5]);
    Result[6] := ;
  end;
end;


mein problem ist diese zeile: SplitRegExpr('#\s+#' , Trim(matches[3]) , Result[5]); denn SplitRegExpr gibt im letzten param ein TStrings zurück obwohl es ja eigentlich ein String sein müsste... wie kann ich jetzt damit ein Preg_Split nachmachen?

Zitat:
procedure SplitRegExpr (const ARegExpr, AInputStr : string; APieces : TStrings);
Zerlegt AInputStr in die Einzelteile APieces getrennt durch die Treffer des Regulären Ausdruckes ARegExpr.


mfG Green
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Di 16.05.06 21:12 
hab jetzt ma weiter probiert (ohne PregSplit) und hab folgendes gebastelt...

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
function TForm1.PregMatch (const AInputString,RegEx : string) : TStringDynArray;
var
   r : TRegExpr;
   i : integer;
begin
   //Result := '';
   SetLength(Result,0);
   r := TRegExpr.Create; { Erzeuge Objekt }
   try { garantiere Speicherfreigabe }
      r.Expression := RegEx;
       { der R.A. wird automatisch in die interne Struktur übersetzt
         innerhalb der Zuweisung an diese Eigenschaft}

      if r.Exec (AInputString) then
         REPEAT
            for i:=0 to high(i) do
               Result[i] := r.Match [i];
         UNTIL not r.ExecNext;
      finally r.Free;
   end;
end;

function TForm1.ParseMessage(const AParseData : string) : TStringDynArray;
  var
    matches,host : TStringDynArray;
begin
  SetLength(matches,0);
  SetLength(matches,0);
  SetLength(Result,0);
  if ExecRegExpr('#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#', AParseData) then begin
    matches := PregMatch(AParseData , '#^(?::(\S+)\s*)?(\w+|\d{3})((?:\s+[^:\s]\S*)*)(?:\s+\:(.*))?\s*$#');
    if ExecRegExpr('#^(.*)!(.*)@(.*)#', matches[1]) then begin
      host := PregMatch(AParseData , '#^(.*)!(.*)@(.*)#');
        Result[0] := host [1];
        Result[1] := host [1];
        Result[2] := host [1];
        Result[3] := host [1];
    end else begin
        Result[0]  := '';
        Result[1]  := '';
        Result[2]  := '';
        Result[3]  := matches[1];
    end;
    Result[4] := matches[1];
    Result[5] := matches[3];
    if matches[4]<>'' then Result[6] := '' else Result[6] := trim(matches[4]);
  end else
    Result[0] := 'false';
end;


aufgerufen wird so:

ausblenden Delphi-Quelltext
1:
2:
3:
        got := ParseMessage(data);
        if got[0] <> 'false' then
          showmessage(got[0]+#10+got[1]+#10+got[2]+#10+got[3]+#10+got[4]+#10+got[5]+#10+got[6]+#10);


syntaktisch alles okay, jedenfalls meckert der compiler nit, aber beim starten kriege ich dann (während es läuft) folgende fehlermeldung:

Zitat:
TRegExpr(comp): Unrecognized Modifier (pos 10)


dabei springt er in die .pas datei der kompo in die zeile:
ausblenden Delphi-Quelltext
1:
 2385:                Error (reeUrecognizedModifier);					


kann mir einer sagen was ich falsch mach??

mfG Green
nullplan001
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win 2000 Professional, Debian Linux 4.0 (Etch,Stable)
Pascal (FreePascal 2.0.2, TurboPascal 7.0), C(++) (G++/GCC 3.4.2 + MinGW), Java (JDK 1.5.0_07), PHP (PHP 5.1.4)
BeitragVerfasst: Mi 17.05.06 12:33 
Öh... klar, eigentlich. Wenn im Quelltext schon steht, dass an der Stelle ein Error gemacht werden soll, wird er auch gemacht. Ist diese Anweisung mit dem Error irgendwie bedingt? Wenn ja, sorge dafür, dass die Bedingung nicht erfüllt ist. Es könnte sein, dass die Kompo ein Problem mit deinen Delimitern hat. Mach doch aus den '#'s einfache '/'s. So habe ich das gelernt. Ist aber theoretisch egal. Jedenfalls sehe ich keinen Modifier, der missverstanden werden könnte. Weis der Kuckuck, was da abgeht.
Tschö,
nullplan

_________________
Ich fahr' nicht selber, weil ich festgestellt habe: ich fahre zu emotional. Bin 180 gefahren wo 30 erlaubt war... -- Jürgen von der Lippe
Grendel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 443

Gentoo Linux, MacOS X, Win 2000
D5 Ent, D7 Ent, Lazarus, Anjuta, MonoDevelop
BeitragVerfasst: Mi 17.05.06 13:08 
user profile iconGreen hat folgendes geschrieben:
kann mir einer sagen was ich falsch mach??

Du nimmst die falsche RegEx-Klasse :P
Im Ernst; Die TRegExpr-Komponente ist nicht wirklich gut und vollständig. Das Problem ist, daß TRegExpr kein capturing, look-ahead und look-behind unterstützt.
Konstruktionen wie z.B. "^(?:a|b|c)$" werden schlicht nicht unterstützt und führen zu einem "Unrecognized Modifier"-Fehler.
Auch sonst ist die Klasse nicht wirklich fehlerfrei.

Ich kann dir die Klasse "TPerlRegEx" empfehlen. Zu finden hier: www.regular-expressions.info/delphi.html
Diese ist ein "einfacher" Wrapper um die PCRE-Lib und unterstützt somit alles, was PCRE auch unterstützt.

Bis neulich ...
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Mi 17.05.06 15:38 
also, ich hab mir die runtergeladen, aber gibts da irgendwo ne hilfe zu? wenn möglich auf deutsch? oder eher ne liste der funktionen und parameter die diese kompo enthält?
Grendel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 443

Gentoo Linux, MacOS X, Win 2000
D5 Ent, D7 Ent, Lazarus, Anjuta, MonoDevelop
BeitragVerfasst: Mi 17.05.06 16:26 
Also ich hab in dem Paket eine Datei namens "PerlRegEx.hlp" ist zwar auf Englisch aber soweit brauchbar.

Im Grunde funktioniert die Komponente sehr ähnlich wie die TRegExpr.

Beispiel für Match (ungetestet):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  reg: TPerlRegEx;
begin
  reg         := TPerlRegEx.Create(nil);
  reg.Subject := 'Der zu untersuchende Text';
  reg.RegEx   := '^(\w)+'; // Der Ausdruck
  if reg.Match() then
  begin
    repeat
      ShowMessage(reg.SubExpressions[1]);  //  0 = gesamter Match, 1 bis n = Teilmatches
    until not reg.MatchAgain();
  end;
  reg.Free();
end;


Bis neulich ...
Green Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 283

Windows XP Home
Delphi 6 Enterprise
BeitragVerfasst: Mi 17.05.06 16:48 
wie krieg ich denn jetzt die gesamte Match menge raus?

mit High(reg.SubExpressions) krieg ich immer '[' erwartet aber ')' gefunden...
Grendel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 443

Gentoo Linux, MacOS X, Win 2000
D5 Ent, D7 Ent, Lazarus, Anjuta, MonoDevelop
BeitragVerfasst: Mi 17.05.06 17:48 
Dafür gibt es eine Eigenschaft.
Ich kann gerade leider nicht nachgucken aber ich meine, daß war irgendetwas in der Art "SubExpressionsCount", "SubExpressions.Count" o.ä.

Guck mal die Eigenschaften in der Doku durch; Sollte eigentlich direkt ins Auge springen.

Bis neulich ...