Reputation: 721
I try to remove dot only from end of each word in given text. (in java) for example:
input: java html. .net node.js php.
output: java html .net node.js php
thanks
Upvotes: 2
Views: 5591
Reputation: 3072
An elaborated solution based on Qtax's answer:
String s = "java html. .net node.js php.";
System.out.println(s);
s = s.replaceAll("(\\w)\\.(?!\\S)", "$1");
System.out.println(s);
Output:
java html .net node.js php
Upvotes: 0
Reputation: 1878
If you're going to use a regular expression, I'd recommend using a word boundary.
\.\B
This matches a literal dot at the end of a word boundary only.
Upvotes: 0
Reputation: 785246
You can do:
String repl = "java html. .net node.js php.".replaceAll("\\.(?!\\w)", "");
// java html .net node.js php
Upvotes: 2
Reputation: 8991
for(String str : input.split(" "))
{
if(str.charAt(str.len - 1) == '.')
str = str.substr(0, str.len - 2);
//do something with str
}
I would avoid regular expressions if at all possible as they are much slower.
Upvotes: 1
Reputation: 33908
Depending on your definition of word you could replace:
(\w)\.(?!\S)
with $1
. Which would remove all .
at the end of a word followed by a space or end of string.
Upvotes: 3