I didn't want to say it in the title, but this question has a little to do with Java. No, please don't leave! It's mainly a regex question, but I am using Java 1.4b3 instead of Perl because I have to, and I could think of no better source of regex-related knowledge than right here at the Monestary.
Ok, now that I am done explaining myself, here is what I need. Say I have a string that looks like:
[g]this would be some text. [n][by]this would be more text.[n]
I would then like to do a search/replace to make the above string:
\u001b[32mthis would be some text...
(Yes, you're are correct in thinking that I'm doing stuff with ANSI codes, and yes, ommitted the rest of the string because I don't want to type out all those codes).
The problem is that Java has nothing like perl's wonderful
s/// that does interpolation - there
is a replace function, and you
can do grouping with paren's in Java, but since all my color codes are
static final String's in seperate inner classes, I can't easily interpolate what I need to replace the
[something] tags with.
Ok, so that might have been a little confusing, especially if you don't know Java. But like I said, you really don't need to, because my real question is about crafting a regular expression to do all the work for me, so I don't have to worry about Java. First of all, Java's regex engine, except for some advanced features, is almost exactly the same as Perl's, so just give me perl regex's.
What I would
like to do is create a regular expression that capture's (using paren's) all the text in the
[...] tags in my string, so I can then look through just the text in those strings and decide what to do (most likely with a large switch statment). I tried:
\[(\w)?(\w)\](.*?)
(Note: there is a miniumum of one, max of two characters in my
[...] tags), but it only matched the FIRST tag, and that was it.
So, is there a clever regex that I can use to do what I need to? Or is there more work that is going to need to be done, regardless of the language?
Thanks a bunch for the help!
r.
j
o
s
e
p
h
"Violence is a last resort of the incompetent" - Salvor Hardin,
Foundation by Issac Asimov
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.