2016-06-01 21 views
0

Ich versuche, Bilder Informationen aus einem Wiki zu bekommen, habe ich eine funktionierende Regex, aber ich scheitere, wenn die Beschreibung auch Markdown hat.Noch eine Regex. Bild von Markdown bekommen, abgehört, wenn Markdown innerhalb

Format der Bilder auf Abschlag:

//[[Image:WilliamGodwin.jpg|thumb|right|150px|William Godwin]] 
//[[Image:JohannMost.jpg|left|150px|thumb|[[Johann Most]] was an outspoken advocate of violence]] 
//[[Image:CNT-armoured-car-factory.jpg|right|thumb|270px|[[Spain]], [[1936]]. Members of the [[CNT]] construct [[armoured car]]s to fight against the [[fascist]]s in one of the [[collectivisation|collectivised]] factories.]] 
[[Image:CNT_tu_votar_y_ellos_deciden.jpg|thumb|175px|CNT propaganda from April 2004. Reads: Don't let the politicians rule our lives/ You vote and they decide/ Don't allow it/ Unity, Action, Self-management.]] 
[[Image:Flag of Anarcho syndicalism.svg|thumb|175px|The red-and-black flag, coming from the experience of anarchists in the labour movement, is particularly associated with anarcho-syndicalism.]] 
[[Image:LeoTolstoy.jpg|thumb|150px|[[Leo Tolstoy|Leo Tolstoy]] 1828-1910]] 

{{Hauptartikel | [[Christian Anarchismus]] und [[Anarchismus und Religion]]}}

Hier ist die versucht: https://regex101.com/r/pD6nF8/1

Ich versuche so etwas wie:

// \[\[Image:(.*?)\|(.*?)\|(.*?)\|(.*?)\|\[*(.*?)\|*(.*?)\]* 
$re = "/\\[\\[Image:(.*?)\\|(.*?)\\|(.*?)\\|(.*?)\\|\\[*(.*?)\\|*(.*?)\\]*/i"; 

Es sollte 14 für diesen Test finden aber ich bekomme 11 so weit, oder wenn ich die 14 bekomme, bekomme ich auch ein paar Geräusche wie]] oder nur Teile der Beschreibung ...

Wie kann ich den optionalen Fall einbeziehen, so etwas zu haben? (. *?)]] im letzten Teil?

+0

Immer Regex-Fragen benötigen einige Beispiele und erwartete Ausgaben. – Shafizadeh

+0

Folgen Sie dem Link auf Regex101, erwartete 14 Links von Bildern, ich kann nur 11 richtig erhalten. Ich habe hier nur die paar für die faulen Leute geschrieben – Cristo

+0

Ich denke, man kann sich nur auf etwas wie ['(\ [\ [Bild: | (?! ^) \ G) \ |? (. *?) Verlassen (? = \ ||]]) '] (https://regex101.com/r/gA7rQ3/3) und füge Logik hinzu, um die Übereinstimmungen in Blöcke zu trennen. Oder [ordnen Sie die Blöcke mit '\ [\ [Image: (. *?) (? =]])'] (Https://regex101.com/r/gA7rQ3/2) zu und teilen Sie sie dann auf. –

Antwort

2

Sie können die verschachtelten Teile definieren, bevor, diese Art von Syntax:

$pattern = '~ 
# definitions 
(?(DEFINE) 
    (?<nested> \[\[ [^][]*+ (?:\[\[ \g<nested> ]] [^][]*)*+ ]]) 
    (?<part> [^][|]*+ (?: \g<nested> [^][|]*)*+   ) 
) 
# main pattern 
\[\[ Image: (\g<part>) \| (\g<part>) \| (\g<part>) \| (\g<part>) \| (\g<part>) ]] 
~ix'; 

demo

Natürlich können Sie genauer sein. Wenn Sie bereits wissen, dass der vierte Teil die Größe ist, können Sie es ersetzen:

\[\[ Image: (\g<part>) \| (\g<part>) \| (\g<part>) \| (\d+ px) \| (\g<part>) ]] 

Sie sind auch frei, um einig Teil optional zu machen, wenn erforderlich (zum Beispiel mit dem Ausrichtungsparameter, die weggelassen werden können):

\[\[ Image: (\g<part>) \| (\g<part>) (?:\| (\g<part>))? \| (\d+ px) \| (\g<part>) ]] 

Oder Sie können sagen, dass alle Parameter sind optional und kann nur einmal vorkommen, aber in diesem Fall müssen Sie genau sein:

~ 
(?(DEFINE) 
    (?<nested> \[\[ [^][]*+ (?: \[\[ \g<nested> ]] [^][]*)*+ ]]) 
    (?<part> [^][|]*+ (?: \g<nested> [^][|]*)*+    ) 
) 

\[\[Image: (?<name> [^]|]*) 
(?: 
    \| 
    (?: (?<align>  left|right|center) | 
     (?<type>  thumb   ) | 
     (?<size>  \d+[a-z]{0,3} ) | 
     (?<description> \g<part>   ) 
    ) 
)* 
]] 
~ix 

demo

+0

Fuuu, das wird immer komplizierter. Es werden jedoch nicht alle Fälle angezeigt, es sollten 14 für diesen Test sein. Scheitert an diesem Beispiel: [Bild: Flagge von Anarcho syndicalism.svg | thumb | 175px | Die rot-schwarze Flagge, die aus der Erfahrung von Anarchisten in der Arbeiterbewegung stammt, ist besonders mit Anarcho-Syndikalismus verbunden.]] – Cristo

+1

@Cristo: Nehmen Sie sich die Zeit, um die Teile optional zu machen, wenn es nötig ist, wie ich es am Ende meiner Antwort zeige. Sie können leicht ein Muster erhalten, das zu allen Fällen passt. Ich werde ein Beispiel eher hinzufügen. –

+0

Ok ich werde morgen mit mehr Zeit versuchen, ich bin schon mehr als 2 Stunden dabei und jetzt ist es spät hier. Aber ich dachte an einen einfacheren Ansatz, den Inline-Marker zu verwenden. Wie alle Fälle scheinen sie in dieselbe Zeile zu passen. Ich denke, das Hauptproblem ist die Beschreibung, der letzte Teil, der innen Markdown hat, nicht die optionalen. – Cristo

0

Ok, wenn ich es richtig verstehe, wollen Sie nur Bilder mit Styling, ohne Beschreibung.

Also ich denke, das funktionieren könnte für Sie

\[\[Image:.*?[jpg|svg][^\s]+(?=\|) 

Dann] fügen Sie einfach], um Ihre Spiele.

1

Beschreibung

Dies ist mehrzeilige regex folgende Fähnchen: Ignorieren Sie Leerzeichen, Global, und Case Insensitive

[[]{2}Image: 
([^|]*\.(?:jpe?g|svg))[|] 
([^|]*)[|] 
    ((?:[[]{2}[^\]]*\]\]|[^|[])*)[|] 
(?:((?:[[]{2}[^\]]*\]\]|[^|[])*)[|])? 
    ((?:[[]{2}[^\]]*\]\]|(?:(?!\]|\|).))*) 
(?:[|]|\]\]) 

Regular expression visualization

Dieser reguläre Ausdruck wird wie folgt vor:

  • finden Sie die [[image:....]] Teilstrings aus m Ihr Beispieltext
  • erfordert, dass das Bild mit einem der folgenden .jpg, .jpeg oder .svg endet. Sie können dieses Verhalten entfernen, indem Sie das \.(?:jpe?g|svg)-Konstrukt entfernen.
  • analysieren die verschiedenen | abgegrenzte Felder
  • Vermeiden schwierigen Randfällen in den letzten mehreren Feldern, die zusätzliche Auszeichnungs

Beispiel

Live Demo

https://regex101.com/r/kI2wE5/2

enthalten

Beispieltext

nahm ich mir die Freiheit, alle 14 Spiele von ziehen, aber die live demo hat immer noch Ihre ursprüngliche Text

[[Image:WilliamGodwin.jpg|thumb|right|150px|William Godwin]] 
[[Image:Pierre_Joseph_Proudhon.jpg|110px|thumb|left|Pierre Joseph Proudhon]] 
[[Image:BenjaminTucker.jpg|thumb|150px|left|[[Benjamin Tucker]]]] 
[[Image:Bakuninfull.jpg|thumb|150px|right|[[Bakunin|Mikhail Bakunin 1814-1876]]]] 
[[Image:PeterKropotkin.jpg|thumb|150px|right|Peter Kropotkin]] 
[[Image:JohannMost.jpg|left|150px|thumb|[[Johann Most]] was an outspoken advocate of violence]] 
[[Image:Flag of Anarcho syndicalism.svg|thumb|175px|The red-and-black flag, coming from the experience of anarchists in the labour movement, is particularly associated with anarcho-syndicalism.]] 
[[Image:CNT_tu_votar_y_ellos_deciden.jpg|thumb|175px|CNT propaganda from April 2004. Reads: Don't let the politicians rule our lives/ You vote and they decide/ Don't allow it/ Unity, Action, Self-management.]] 
[[Image:CNT-armoured-car-factory.jpg|right|thumb|270px|[[Spain]], [[1936]]. Members of the [[CNT]] construct [[armoured car]]s to fight against the [[fascist]]s in one of the [[collectivisation|collectivised]] factories.]] 
[[Image:LeoTolstoy.jpg|thumb|150px|[[Leo Tolstoy|Leo Tolstoy]] 1828-1910]] 
[[Image:Goldman-4.jpg|thumb|left|150px|[[Emma Goldman]]]] 
[[Image:Murray Rothbard Smile.JPG|thumb|left|150px|[[Murray Rothbard]] (1926-1995)]] 
[[Image:Hakim Bey.jpeg|thumb|right|[[Hakim Bey]]]] 
[[Image:Noam_chomsky.jpg|thumb|150px|right| [[Noam Chomsky]] (1928–)]] 

Probe Spiele

[0][0] = [[Image:WilliamGodwin.jpg|thumb|right|150px|William Godwin]] 
[0][1] = WilliamGodwin.jpg 
[0][2] = thumb 
[0][3] = right 
[0][4] = 150px 
[0][5] = William Godwin 

[1][0] = [[Image:Pierre_Joseph_Proudhon.jpg|110px|thumb|left|Pierre Joseph Proudhon]] 
[1][1] = Pierre_Joseph_Proudhon.jpg 
[1][2] = 110px 
[1][3] = thumb 
[1][4] = left 
[1][5] = Pierre Joseph Proudhon 

[2][0] = [[Image:BenjaminTucker.jpg|thumb|150px|left|[[Benjamin Tucker]]]] 
[2][1] = BenjaminTucker.jpg 
[2][2] = thumb 
[2][3] = 150px 
[2][4] = left 
[2][5] = [[Benjamin Tucker]] 

[3][0] = [[Image:Bakuninfull.jpg|thumb|150px|right|[[Bakunin|Mikhail Bakunin 1814-1876]]]] 
[3][1] = Bakuninfull.jpg 
[3][2] = thumb 
[3][3] = 150px 
[3][4] = right 
[3][5] = [[Bakunin|Mikhail Bakunin 1814-1876]] 

[4][0] = [[Image:PeterKropotkin.jpg|thumb|150px|right|Peter Kropotkin]] 
[4][1] = PeterKropotkin.jpg 
[4][2] = thumb 
[4][3] = 150px 
[4][4] = right 
[4][5] = Peter Kropotkin 

[5][0] = [[Image:JohannMost.jpg|left|150px|thumb|[[Johann Most]] was an outspoken advocate of violence]] 
[5][1] = JohannMost.jpg 
[5][2] = left 
[5][3] = 150px 
[5][4] = thumb 
[5][5] = [[Johann Most]] was an outspoken advocate of violence 

[6][0] = [[Image:Flag of Anarcho syndicalism.svg|thumb|175px|The red-and-black flag, coming from the experience of anarchists in the labour movement, is particularly associated with anarcho-syndicalism.]] 
[6][1] = Flag of Anarcho syndicalism.svg 
[6][2] = thumb 
[6][3] = 175px 
[6][4] = 
[6][5] = The red-and-black flag, coming from the experience of anarchists in the labour movement, is particularly associated with anarcho-syndicalism. 

[7][0] = [[Image:CNT_tu_votar_y_ellos_deciden.jpg|thumb|175px|CNT propaganda from April 2004. Reads: Don't let the politicians rule our lives/ You vote and they decide/ Don't allow it/ Unity, Action, Self-management.]] 
[7][1] = CNT_tu_votar_y_ellos_deciden.jpg 
[7][2] = thumb 
[7][3] = 175px 
[7][4] = 
[7][5] = CNT propaganda from April 2004. Reads: Don't let the politicians rule our lives/ You vote and they decide/ Don't allow it/ Unity, Action, Self-management. 

[8][0] = [[Image:CNT-armoured-car-factory.jpg|right|thumb|270px|[[Spain]], [[1936]]. Members of the [[CNT]] construct [[armoured car]]s to fight against the [[fascist]]s in one of the [[collectivisation|collectivised]] factories.]] 
[8][1] = CNT-armoured-car-factory.jpg 
[8][2] = right 
[8][3] = thumb 
[8][4] = 270px 
[8][5] = [[Spain]], [[1936]]. Members of the [[CNT]] construct [[armoured car]]s to fight against the [[fascist]]s in one of the [[collectivisation|collectivised]] factories. 

[9][0] = [[Image:LeoTolstoy.jpg|thumb|150px|[[Leo Tolstoy|Leo Tolstoy]] 1828-1910]] 
[9][1] = LeoTolstoy.jpg 
[9][2] = thumb 
[9][3] = 150px 
[9][4] = 
[9][5] = [[Leo Tolstoy|Leo Tolstoy]] 1828-1910 

[10][0] = [[Image:Goldman-4.jpg|thumb|left|150px|[[Emma Goldman]]]] 
[10][1] = Goldman-4.jpg 
[10][2] = thumb 
[10][3] = left 
[10][4] = 150px 
[10][5] = [[Emma Goldman]] 

[11][0] = [[Image:Murray Rothbard Smile.JPG|thumb|left|150px|[[Murray Rothbard]] (1926-1995)]] 
[11][1] = Murray Rothbard Smile.JPG 
[11][2] = thumb 
[11][3] = left 
[11][4] = 150px 
[11][5] = [[Murray Rothbard]] (1926-1995) 

[12][0] = [[Image:Hakim Bey.jpeg|thumb|right|[[Hakim Bey]]]] 
[12][1] = Hakim Bey.jpeg 
[12][2] = thumb 
[12][3] = right 
[12][4] = 
[12][5] = [[Hakim Bey]] 

[13][0] = [[Image:Noam_chomsky.jpg|thumb|150px|right| [[Noam Chomsky]] (1928–)]] 
[13][1] = Noam_chomsky.jpg 
[13][2] = thumb 
[13][3] = 150px 
[13][4] = right 
[13][5] = [[Noam Chomsky]] (1928–) 

Erklärung

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    [[]{2}     any character of: '[' (2 times) 
---------------------------------------------------------------------- 
    Image:     'Image:' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [^|]*     any character except: '|' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    \.      '.' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
     jp      'jp' 
---------------------------------------------------------------------- 
     e?      'e' (optional (matching the most 
           amount possible)) 
---------------------------------------------------------------------- 
     g      'g' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     svg      'svg' 
---------------------------------------------------------------------- 
    )      end of grouping 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    [|]      any character of: '|' 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [^|]*     any character except: '|' (0 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
    [|]      any character of: '|' 
---------------------------------------------------------------------- 
    (      group and capture to \3: 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     [[]{2}     any character of: '[' (2 times) 
---------------------------------------------------------------------- 
     [^\]]*     any character except: '\]' (0 or more 
           times (matching the most amount 
           possible)) 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     [^|[]     any character except: '|', '[' 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
)      end of \3 
---------------------------------------------------------------------- 
    [|]      any character of: '|' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    (      group and capture to \4: 
---------------------------------------------------------------------- 
     (?:      group, but do not capture (0 or more 
           times (matching the most amount 
           possible)): 
---------------------------------------------------------------------- 
     [[]{2}     any character of: '[' (2 times) 
---------------------------------------------------------------------- 
     [^\]]*     any character except: '\]' (0 or 
           more times (matching the most amount 
           possible)) 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
     |      OR 
---------------------------------------------------------------------- 
     [^|[]     any character except: '|', '[' 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
    )      end of \4 
---------------------------------------------------------------------- 
    [|]      any character of: '|' 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    (      group and capture to \5: 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     [[]{2}     any character of: '[' (2 times) 
---------------------------------------------------------------------- 
     [^\]]*     any character except: '\]' (0 or more 
           times (matching the most amount 
           possible)) 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
     \]      ']' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     (?:      group, but do not capture: 
---------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
      \]      ']' 
---------------------------------------------------------------------- 
     |      OR 
---------------------------------------------------------------------- 
      \|      '|' 
---------------------------------------------------------------------- 
     )      end of look-ahead 
---------------------------------------------------------------------- 
     .      any character except \n 
---------------------------------------------------------------------- 
    )      end of grouping 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
)      end of \5 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
    [|]      any character of: '|' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
    \]      ']' 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
+0

Erstaunliche Verwendung von Regex und Klarheit der Erklärung, aber gegeben 2 Entscheidungen, die das gute Ergebnis erzielen Ich gehe für das optimierte. Euer braucht 7x mehr Zeit als Casimir. Ich werde das als Referenz nehmen, falls in den großen Tests ein Randfall auftritt. Vielen Dank für Ihre Zeit – Cristo

+0

Ich stimme völlig zu, Casimir et Hippolyte Ausdruck ist viel eleganter. Ich mag die Verwendung von Unterroutinenausdrücken, aber sie werden nicht in allen Sprachen unterstützt. Also lasse ich meine Antwort hier für den Fall, dass jemand, der eine andere Sprache benutzt, etwas Ähnliches braucht. –

0

Was ist, wenn Sie sie nur mit dieser Regex übereinstimmen: \[\[Image\:(.*)\]\] und dann teilen Sie jedes Ergebnis einfach mit |. Ich weiß nicht, ob es eine gute Idee ist, aber es schadet nichts.