string pattern replacement

J

jagonzal

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

Oliver Wong

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
 
R

Rhino

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

Carl

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

Carl

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

jagonzal

Carl said:
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!
 
N

Neil Padgen

Carl said:
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
 
O

Oliver Wong

Neil Padgen said:
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"?
 
R

Robert Klemme

Neil said:
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
 
L

Lasse Reichstein Nielsen

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
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top