Java RegEx - Please help

Discussion in 'Java' started by GRecken, Oct 21, 2003.

  1. GRecken

    GRecken Guest

    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
     
    GRecken, Oct 21, 2003
    #1
    1. Advertising

  2. GRecken

    Paul Lutus Guest

    GRecken wrote:

    > 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.

    < snip>

    > 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.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 21, 2003
    #2
    1. Advertising

  3. GRecken

    Roedy Green Guest

    Roedy Green, Oct 21, 2003
    #3
  4. GRecken

    Phil Hanna Guest

    On 21 Oct 2003 10:40:19 -0700, (GRecken) wrote:

    >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.
    --
    Phil Hanna
    Author of JSP 2.0: The Complete Reference
    http://www.philhanna.com
     
    Phil Hanna, Oct 23, 2003
    #4
  5. GRecken

    Roedy Green Guest

    On Thu, 23 Oct 2003 02:36:58 GMT, (Phil Hanna) wrote
    or quoted :

    > 2. Double all the backslashes and insert backslashes before any
    >quote characters:
    >
    > \\[!!!([^\\]]*)\\]


    the quoter amanuensis will automate this for you.

    See http://mindprod.com/quoter.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 23, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  2. Replies:
    4
    Views:
    521
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    595
    Big Brian
    Oct 14, 2003
  4. Replies:
    3
    Views:
    772
    Reedick, Andrew
    Jul 1, 2008
  5. Daku
    Replies:
    4
    Views:
    737
    Daniel Pitts
    Nov 18, 2009
Loading...

Share This Page