string pattern replacement

Discussion in 'Java' started by jagonzal@gmail.com, Mar 8, 2006.

  1. Guest

    Hi,

    I'm trying to find instances of a period separating two words with no
    space within a string, and replace such periods with a space. For
    example, if I give it this string:

    "I'm a string. With.some.period.separated.words"

    it should return

    "I'm a string. With some period separated words"

    The first period stays because there was a space to its right. The
    other ones are replaced.

    I tried doing this:

    String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    \2");

    This results in: "I'm a string. Wit* *om* *erio* *eparate* *ords", the
    asterisks denoting non-existing characters in the printing font
    (unicode 1 and 2?)

    Then I tried this, supposing I was not escaping the back references to
    the capturing groups correctly:

    String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    \2");

    This results in: "I'm a string. Wit1 2om1 2erio1 2eparate1 2ords"

    Anybody spot what am I doing wrong?

    Thanks in advance,

    Javier.
     
    , Mar 8, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to find instances of a period separating two words with no
    > space within a string, and replace such periods with a space. For
    > example, if I give it this string:
    >
    > "I'm a string. With.some.period.separated.words"
    >
    > it should return
    >
    > "I'm a string. With some period separated words"
    >
    > The first period stays because there was a space to its right. The
    > other ones are replaced.
    >
    > I tried doing this:
    >
    > String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    > \2");
    >
    > This results in: "I'm a string. Wit* *om* *erio* *eparate* *ords", the
    > asterisks denoting non-existing characters in the printing font
    > (unicode 1 and 2?)
    >
    > Then I tried this, supposing I was not escaping the back references to
    > the capturing groups correctly:
    >
    > String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    > \2");
    >
    > This results in: "I'm a string. Wit1 2om1 2erio1 2eparate1 2ords"
    >
    > Anybody spot what am I doing wrong?


    Your first and second example seem identical to me (same escaping-level
    for back references).

    If you can't get your head around the RegEx required (and I admit I'm
    having trouble too), why not just code it with a simple for-loop that scans
    your string search for periods, then checking the appropriate conditions,
    then replacing the period with space where appropriate? This code will be
    longer, but probably easier to understand and maintain.

    - Oliver
     
    Oliver Wong, Mar 8, 2006
    #2
    1. Advertising

  3. Rhino Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to find instances of a period separating two words with no
    > space within a string, and replace such periods with a space. For
    > example, if I give it this string:
    >
    > "I'm a string. With.some.period.separated.words"
    >
    > it should return
    >
    > "I'm a string. With some period separated words"
    >
    > The first period stays because there was a space to its right. The
    > other ones are replaced.
    >
    > I tried doing this:
    >
    > String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    > \2");
    >
    > This results in: "I'm a string. Wit* *om* *erio* *eparate* *ords", the
    > asterisks denoting non-existing characters in the printing font
    > (unicode 1 and 2?)
    >
    > Then I tried this, supposing I was not escaping the back references to
    > the capturing groups correctly:
    >
    > String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    > \2");
    >
    > This results in: "I'm a string. Wit1 2om1 2erio1 2eparate1 2ords"
    >
    > Anybody spot what am I doing wrong?
    >
    > Thanks in advance,
    >

    I'm not very good with regular expressions so I tried to find a solution to
    your question just for the practice. This method works, but it's pretty
    ugly. I'm going to post it here anyway, just in case you're stuck and are
    willing to use an ugly solution:

    String originalString = "I'm a string. With.some.period.separated.words";

    //Replace all periods followed by spaces with percent signs followed by
    spaces.

    String filteredString1 = originalString.replaceAll("\\. ", "% ");

    System.out.println("filteredString1 = " + filteredString1);

    //Replace all remaining periods with spaces.

    String filteredString2 = filteredString1.replaceAll("\\.", " ");

    System.out.println("filteredString2 = " + filteredString2);

    //Replace all percents followed by spaces to periods followed by spaces.

    String filteredString3 = filteredString2.replaceAll("% ", "\\. ");

    System.out.println("filteredString3 = " + filteredString3);

    I'll try to think of a solution that does all of the work in a single
    replaceAll but I don't promise to find one; this will serve as a workable
    solution if no one else finds one either ;-)

    By the way, in your original question, you seem to have the exact same
    replaceAll arguments in both examples that you give which must be a
    mistake....

    --

    Rhino
     
    Rhino, Mar 8, 2006
    #3
  4. Carl Guest

    Javier,

    How about this:

    String s = "I'm a string. With.some.period.separated.words.";
    Matcher m = Pattern.compile("(\\S)\\.(\\S)").matcher(s);
    String s2 = m.replaceAll("$1 $2");
    System.out.println(s2);

    Hope that helps,
    Carl.
     
    Carl, Mar 9, 2006
    #4
  5. Carl Guest

    Doh!
    You can actually make it shorter:

    String s = "I'm a string. With.some.period.separated.words.";
    String s2 = s.replaceAll("(\\S)\\.(\\S)", "$1 $2");

    Carl.
     
    Carl, Mar 9, 2006
    #5
  6. Guest

    Carl wrote:
    > Doh!
    > You can actually make it shorter:
    >
    > String s = "I'm a string. With.some.period.separated.words.";
    > String s2 = s.replaceAll("(\\S)\\.(\\S)", "$1 $2");
    >
    > Carl.


    Thank you, all that contributed with your solutions.

    Carl's method works - I thought that "\n" would replace with the n-th
    matching group, not "$n". Oh well, I guess it's to be expected when one
    learns regexp by using perl :)

    Thanks again, everyone!
     
    , Mar 9, 2006
    #6
  7. Neil Padgen Guest

    Carl wrote:

    > Doh!
    > You can actually make it shorter:
    >
    > String s = "I'm a string. With.some.period.separated.words.";
    > String s2 = s.replaceAll("(\\S)\\.(\\S)", "$1 $2");


    Shorter still: you don't need that first capturing group at all.

    String s = "I'm a string. With.some.period.separated.words.";
    String s2 = s.replaceAll("\\.(\\S)", " $1");

    -- Neil
     
    Neil Padgen, Mar 9, 2006
    #7
  8. Oliver Wong Guest

    "Neil Padgen" <> wrote in message
    news:dupi64$hqu$...
    > Carl wrote:
    >
    >> Doh!
    >> You can actually make it shorter:
    >>
    >> String s = "I'm a string. With.some.period.separated.words.";
    >> String s2 = s.replaceAll("(\\S)\\.(\\S)", "$1 $2");

    >
    > Shorter still: you don't need that first capturing group at all.
    >
    > String s = "I'm a string. With.some.period.separated.words.";
    > String s2 = s.replaceAll("\\.(\\S)", " $1");


    Won't this latter pattern erroneously transform "hello .world" into
    "hello world"?
     
    Oliver Wong, Mar 9, 2006
    #8
  9. Neil Padgen wrote:
    > Carl wrote:
    >
    >> Doh!
    >> You can actually make it shorter:
    >>
    >> String s = "I'm a string. With.some.period.separated.words.";
    >> String s2 = s.replaceAll("(\\S)\\.(\\S)", "$1 $2");

    >
    > Shorter still: you don't need that first capturing group at all.
    >
    > String s = "I'm a string. With.some.period.separated.words.";
    > String s2 = s.replaceAll("\\.(\\S)", " $1");


    Actually it's even simpler with lookahead:

    s.replaceAll( "\\.(?=\\S)", " " )

    Kind regards

    robert
     
    Robert Klemme, Mar 9, 2006
    #9
  10. writes:

    > I'm trying to find instances of a period separating two words with no
    > space within a string, and replace such periods with a space.


    That description sounds like it would be matched by the RegExp:
    "\\b\\.\\b"
    i.e., a period between two words. The "\b" escape is a word boundary
    match - a zero-width match when one adjacent character is a word character
    and the other isn't.

    ....
    > Then I tried this, supposing I was not escaping the back references to
    > the capturing groups correctly:


    > String filteredString = originalString.replaceAll("(\\S)\\.(\\S)","\1
    > \2")


    (there was no difference from the first example. Did you write \01 and \02
    originally?)

    > This results in: "I'm a string. Wit1 2om1 2erio1 2eparate1 2ords"
    >
    > Anybody spot what am I doing wrong?


    You are not escaping your backslashes. Try "\\1 \\2".

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Mar 12, 2006
    #10
    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. Mladen Adamovic
    Replies:
    0
    Views:
    764
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,726
    Mladen Adamovic
    Dec 5, 2003
  3. Jarkko Viinamäki
    Replies:
    1
    Views:
    4,247
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    Feb 22, 2004
  4. Replies:
    17
    Views:
    1,913
    Chris Uppal
    Nov 16, 2005
  5. ChrisC
    Replies:
    4
    Views:
    185
    ChrisC
    Jun 25, 2010
Loading...

Share This Page