ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
3399
3399
EMPFEHLEN
-
21.01.08 16:01 #1
- Registriert seit
- Sep 2003
- Beiträge
- 85
Hallo,
mein String beinhaltet eckige Klammern. Ich würde gerne per REGEX die Klammern durch spitze Klammern ersetzen.
Meine Jetzige Lösung gibt das folgende Ergebnis aus:Zur Zeit wird nur wort2 ersetzt :/PHP-Code:[b]wort1[/b] <b>wort3</b> [i]wort3[i]
Ich hoffe, ihr könnt mir weiter helfen.
Bedanke mich im voraus
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13
public static void main(String[] args) { String str = "[b]wort1[/b] [b]wort3[/b] [i]wort3[i]"; Pattern pBold = Pattern.compile(".*(\\[b\\](.*)\\[/b\\]).*"); Matcher mBold = pBold.matcher(str); if(mBold.matches()) { String htmlString = "<b>"+mBold.group(2)+"</b>"; str = str.replace(mBold.group(1), htmlString); System.out.println(str); } }
-
21.01.08 16:58 #2kaMii Tutorials.de Gastzugang
Dein Pattern überzeugt mich nicht ganz, und mit split() biste besser dran:
Code java: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
import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String str = "[b]wort1[/b] [b]wort2[/b] [i]wort3[/i] [h1]wort4[/h1]"; Pattern pBold = Pattern.compile("\\[|\\]"); pBold.matcher(str); String[] singledata = pBold.split(str); // set up new string String result = ""; for(int j = 0; j < singledata.length; j++){ if(!singledata[j].isEmpty()){ result += ("<" + singledata[j] + ">" + singledata[j+1] + "<" + singledata[j+2] + "> "); j = j + 3; } } System.out.println(result); } }
Ich hoffe ich konnte helfen!
Gruss Ben
PS: Wenn sich das Problem erledigt hat bitte in dem Thread hier den "Erledigt"-Button drücken, thx
Geändert von kaMii (22.01.08 um 11:24 Uhr)
Ähnliche Themen
-
public String replaceAll(String regex, String replacement) und das $-Zeichen
Von takidoso im Forum JavaAntworten: 5Letzter Beitrag: 22.11.10, 17:31 -
Regex für String.split(regex)
Von anti43 im Forum JavaAntworten: 3Letzter Beitrag: 22.11.10, 12:14 -
RegEx Generator, vom String zum RegEx?
Von tequila slammer im Forum .NET CaféAntworten: 11Letzter Beitrag: 15.11.10, 07:58 -
Mittels Regex Gültigkeit einer anderen Regex prüfen
Von blade runner im Forum PHPAntworten: 8Letzter Beitrag: 25.06.10, 12:07 -
RegEx String Problem...
Von Andreas Gaisbauer im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 08.01.03, 14:34





Zitieren
Login




