Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   php und reguläre Ausdrücke (https://www.delphipraxis.net/58369-php-und-regulaere-ausdruecke.html)

yankee 6. Dez 2005 03:22


php und reguläre Ausdrücke
 
Hi @all, ich habe mir gerade ein Tutorial in php über reguläre Ausdrücke reingezogen und wollte das in php jetzt anwenden.
Und ich habe mir das in etwas zu vorgestellt, dass ich einen string in der Art habe
abcdabdkh[5]alhkjafda[14]akjnf
Und ich will jetzt die Zahlen in den eckigen klammern finden. Das müsste ich also mit diesem Ausdruck können:
.*\[(\d+)\].*\[(\d+)\]
Also das jetzt nur mal als Beispiel, meine Zeichenketten sind noch etwas komplexer...
Naja, in dem Programm "The Regex Coach" funktioniert das. Aber wie setze ich das in php um? Ich habe als erstes an preg_split() gedacht, aber das leifert nicht wirklich was ich will.
Irgendjemand eine Idee?
-----
EDIT:
Ok, ich hab'S nach langer googlei und testelei doch noch rausbekommen:
Code:
  $var =Array();
  preg_match('/.*\[(\d+)\].*\[(\d+)\]/','iljhieff[5]djhsfasd[12]',$var);
  print_r($var);
wäre die korrekte umsetzung. Dabei ist es mit völlig schleierhaft, warum die in php immer ein slash (/) an den anfang und ende setzen...

jim_raynor 6. Dez 2005 06:18

Re: php und reguläre Ausdrücke
 
Zitat:

Zitat von yankee
Dabei ist es mit völlig schleierhaft, warum die in php immer ein slash (/) an den anfang und ende setzen...

Weil dies der Standard bei Regulären Ausdrücken ist. Du kannst auch statt / ein ! oder "a" oder was auch immer nehmen. Nach dem Schliessenden Zeichen kannst du dann noch paar Einstellungen machen, wie zum Beispiel:
Zitat:

Zitat von PHP-Handbuch
i (PCRE_CASELESS)
If this modifier is set, letters in the pattern match both upper and lower case letters.

m (PCRE_MULTILINE)
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl.

When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

und noch ein paar Mehr. Musst mal im PHP-Handbuch unter Reguläre Ausdrücke suchen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz