• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression replacing [ ]

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write a function to process my own UBB code, so (ignore spaces)

[ I M G ] myImage.gif [ / I M G ]

Would change to the html for an image. However the regular expression for replacing square brackets seems not to have been thought about. Below is some quick code that outlines what i want to do and the problem. Shove it in a JSP page:

<%! String foo = "[img]"; %>
<%! String result = ""; %>
<%= foo %>
<%
result = foo.replaceAll("[img]","-");
%>
<%= result%>


And the output is

[---]

So it is not replacing the '[' and ']' . This I understand is because '[' and ']' are used in regular expressions. However one cannot escape them because an error is thrown:

illegal escape character

What to do?
[ December 29, 2004: Message edited by: Jack Wootton ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you try \[IMG\]
 
Jack Wootton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still, illegal escape character
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you have to do is escape the \ chacter so the expression should be:

\\[IMG\\]

The reson for the illegal escape character is Java doesn't understand \[
 
Jack Wootton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked. Thank you.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really JSP-related, so even though resolved I'm moving this along so it can be available for future reference in a more appropriate forum.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic