Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Basic Regex question with a

by r.joseph (Hermit)
on Dec 22, 2001 at 05:32 UTC ( [id://133926]=perlquestion: print w/replies, xml ) Need Help??

r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Basic Regex question with a
by r.joseph (Hermit) on Dec 23, 2001 at 01:22 UTC
    I hereby rescind this evil and tainted question, as I have been clued in (thanks crazyinsomniac) to a Java-related, PM-like community over at JavaJunkies.org. Check here for my post on JJ.org...if you care. Sorry about the post gone bad.

    r. j o s e p h
    "Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://133926]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found