Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

regex_replace ersetzt nur letzten Match

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
niehztog
Smarty n00b


Joined: 23 Dec 2008
Posts: 2

PostPosted: Tue Dec 23, 2008 10:14 am    Post subject: regex_replace ersetzt nur letzten Match Reply with quote

Hi,

ich bin auch relativ neu was den Umgang mit Smarty-Templates angeht, deswegen gleich zu Anfang mal eine Frage:

Ich habe diesen Ausdruck geschrieben, der in einem mehrzeiligen Text den Part, der zwischen zwei Rauten steht, verlinken soll. Hier mein Ansatz:
Code:
{$text|regex_replace:'/^(.*)#(.*)#(.*)$/':'\\1<a href="link.html">\\2</a>\\3'}


Leider, leider gibt es im Text zwei in Rauten eingefasste Stellen, die verlinkt werden sollen, und der Reguläre Ausdruck ersetzt immer nur die letzte.
Nur ungern würde ich den Regulären Ausdruck so abändern, dass er immer exakt zwei Vorkommnisse von #Link# erwartet. Eigentlich sollte regex_replace doch immer alle Matches des regulären Ausdrucks ersetzen?

Wo liegt der Fehler?

Freundliche Grüße
Niehztog
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Dec 29, 2008 9:17 am    Post subject: Reply with quote

Versuch mal den Modifier m:
Quote:
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.

_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
niehztog
Smarty n00b


Joined: 23 Dec 2008
Posts: 2

PostPosted: Mon Jan 05, 2009 8:33 am    Post subject: Reply with quote

Funktioniert leider nicht, zumal in dem Text auch keine Zeilenumbrüche drin sind (wird ja in HTML Darstellung automatisch umgebrochen).
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jan 05, 2009 11:18 am    Post subject: Reply with quote

(.*) Ist per default greedy, also matched #(.*)# alles von der ersten bis zur letzten Raute (inkl. aller Rauten). du willst wahrscheinlich #(.*?)# - also non-greedy Matching.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP