3 Stimmen

php regex - saubere Dateinamen

Ich bin auf der Suche nach einem eleganten regulären Ausdruck für saubere Klammern mit Inhalt sieht aus wie Dateiname.

[Nibh justo] elit Nulla [link.pdf]  auctor ipsum molestie (link.pdf) 
Condimentum euismod non [link.xls](link.xls) [link.doc](link.doc) tempus 
In [Curabitur] et

Das Ergebnis sollte sein:

Nibh justo elit Nulla auctor ipsum molestie Condimentum euismod 
non tempus In Curabitur et

Ich bin sicher, dass es dafür einen kurzen Weg gibt. (Datei bedeutet einfach - Punkt eingeschlossen. Es ist keine Satzprüfung notwendig.)

danke für die Hilfe

3voto

Julien Punkte 1286

Etwa so?

$str = '[Nibh justo] elit Nulla [link.pdf]  auctor ipsum molestie (link.pdf) 
Condimentum euismod non [link.xls](link.xls) [link.doc](link.doc) tempus In 
[Curabitur] and other [./beta/link.pfd]';

$str = preg_replace('`(\(|\[)[\w/\.-]+\.[a-z]+(\)|\])`i', '', $str);
$str = str_replace(array('[', ']'), '', $str);

echo $str;

Das Ergebnis ist:

Nibh justo elit Nulla auctor ipsum molestie Condimentum euismod 
non tempus In Curabitur and other

2voto

Cylian Punkte 10460

Versuchen Sie dies:

(?:[\[\(]\w+\.\w+[\]\)])|(?:[\[\(](?=[0-9A-Za-z]))|(?:(?<=[0-9A-Za-z])[\]\)])

$result = preg_replace('/(?:[[(]\w+\.\w+[\])])|(?:[[(](?=[0-9A-Za-z]))|(?:(?<=[0-9A-Za-z])[\])])/m', '', $subject);

Erläuterung:

    <!--
(?:[\[\(]\w+\.\w+[\]\)])|(?:[\[\(](?=[0-9A-Za-z]))|(?:(?<=[0-9A-Za-z])[\]\)])

Options: ^ and $ match at line breaks

Match either the regular expression below (attempting the next alternative only if this one fails) «(?:[\[\(]\w+\.\w+[\]\)])»
   Match the regular expression below «(?:[\[\(]\w+\.\w+[\]\)])»
      Match a single character present in the list below «[\[\(]»
         A [ character «\[»
         A ( character «\(»
      Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Match the character “.” literally «\.»
      Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Match a single character present in the list below «[\]\)]»
         A ] character «\]»
         A ) character «\)»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(?:[\[\(](?=[0-9A-Za-z]))»
   Match the regular expression below «(?:[\[\(](?=[0-9A-Za-z]))»
      Match a single character present in the list below «[\[\(]»
         A [ character «\[»
         A ( character «\(»
      Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[0-9A-Za-z])»
         Match a single character present in the list below «[0-9A-Za-z]»
            A character in the range between “0” and “9” «0-9»
            A character in the range between “A” and “Z” «A-Z»
            A character in the range between “a” and “z” «a-z»
Or match regular expression number 3 below (the entire match attempt fails if this one fails to match) «(?:(?<=[0-9A-Za-z])[\]\)])»
   Match the regular expression below «(?:(?<=[0-9A-Za-z])[\]\)])»
      Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=[0-9A-Za-z])»
         Match a single character present in the list below «[0-9A-Za-z]»
            A character in the range between “0” and “9” «0-9»
            A character in the range between “A” and “Z” «A-Z»
            A character in the range between “a” and “z” «a-z»
      Match a single character present in the list below «[\]\)]»
         A ] character «\]»
         A ) character «\)»
-->

wenn das obige RegEx auf :

[Nibh justo] elit Nulla [link.pdf]  auctor ipsum molestie (link.pdf) 
Condimentum euismod non [link.xls](link.xls) [link.doc](link.doc) tempus 
In [Curabitur] et

führt zum gewünschten Ergebnis:

Nibh justo elit Nulla   auctor ipsum molestie  
Condimentum euismod non   tempus 
In Curabitur et

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X