Java RegEx - Please help

G

GRecken

Is it just me or is this very confusing????

All I want to do is to take a string and do a search and replace.

My string will look something like:

fdafsadfs[!!!VALUE]fdklajdkljafkldjlkfajslksd

And I want to find the [!!!VALUE] and replace it with the value from the database.

My code is very skimpy and far from close.


**********

String str1 = "fda!fsadfs[!!!VALUE]fdklajdkljafkldjlkfajslk!sd"

Pattern p = Pattern.compile("[!!!(.*?)]");

String newStr = t1.replaceAll( p.pattern(), "NEWVALUE" );

*********************


But this replaces all of the "!" with "NEWVALUE" which results in:

fdaNEWVALUEfsadfs[NEWVALUENEWVALUENEWVALUEVALUE]fdklajdkljafkldjlkfajslkNEWVALUEsd


Can anyone please point me in the right direction?

Thanks,
Greg
 
P

Paul Lutus

GRecken said:
Is it just me or is this very confusing????

All I want to do is to take a string and do a search and replace.

My string will look something like:

fdafsadfs[!!!VALUE]fdklajdkljafkldjlkfajslksd

And I want to find the [!!!VALUE] and replace it with the value from the
database.

My code is very skimpy and far from close.


**********

String str1 = "fda!fsadfs[!!!VALUE]fdklajdkljafkldjlkfajslk!sd"

Pattern p = Pattern.compile("[!!!(.*?)]");

Ths doesn't do what you think it does.

Can anyone please point me in the right direction?

Some characters have special meanings to the regex engine. Your test string
has several meta-characters. Just escape each meta-character you want
treated as literal:

Pattern p = Pattern.compile("\\[!!!(.*?)\\]");

But you must decide which characters are to be treated as literals and which
as meta-characters. To do this effectively, you need to ... read the
documentation.
 
P

Phil Hanna

Is it just me or is this very confusing????
All I want to do is to take a string and do a search and replace.
My string will look something like:
fdafsadfs[!!!VALUE]fdklajdkljafkldjlkfajslksd
And I want to find the [!!!VALUE] and replace it with the value from the database.

String str1 = "fda!fsadfs[!!!VALUE]fdklajdkljafkldjlkfajslk!sd"
Pattern p = Pattern.compile("[!!!(.*?)]");
String newStr = t1.replaceAll( p.pattern(), "NEWVALUE" );

Guilty pleasure:

When I have to write non-trivial regular expressions in Java, I write
and debug them first in Perl:

#! perl -w
use strict;

my $str1 = "fda!fsadfs[!!!VALUE]fdklajdkljafkldjlkfajslk!sd";
$str1 =~ s/\[!!!([^\]]*)\]/NEWVALUE/g;
print "$str1\n";

The write/test/fiddle-with cycle is much shorter.

Once I've got that working reliably, I convert the pattern to Java:

1. Take what's between the first set of slashes:

\[!!!([^\]]*)\]

2. Double all the backslashes and insert backslashes before any
quote characters:

\\[!!!([^\\]]*)\\]

3. Enclose the result in quotes and compile the pattern:

Pattern p = Pattern.compile("\\[!!!([^\\]]*)\\]");


There are a few minor differences between Perl5 and Java regex'es, but
they are well documented in the javadoc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top