Reputation: 57
Please help me out to get the specific regex
to remove comma after a word pattern in java.
Assume, I would like to delete comma after each pattern where the pattern is <Word$TAG>, <Word$TAG>, <Word$TAG>, <Word$TAG>, <Word$TAG>
now I want my output to be <Word$TAG> <Word$TAG> <Word$TAG> <Word$TAG>
. if I used .replaceAll()
, it will replace all commas, but in my <Word$TAG>
Word may have a comma(,).
For example, Input.txt is as follows
mms§NNP_ACRON, site§N_NN, pe§PSP, ,,,,,§RD_PUNC, link§N_NN, ....§RD_PUNC, CID§NNP_ACRON, team§N_NN, :)§E
and Output.txt
mms§NNP_ACRON site§N_NN pe§PSP ,,,,,§RD_PUNC link§N_NN ....§RD_PUNC CID§NNP_ACRON team§N_NN :)§E
Upvotes: 0
Views: 992
Reputation: 174706
You could use a positive lookahead assertion to match all the commas which are followed by a space or end of the line anchor.
String s = "mms§NNP_ACRON, site§N_NN, pe§PSP, ,,,,,§RD_PUNC, link§N_NN, ....§RD_PUNC, CID§NNP_ACRON, team§N_NN, :)§E";
System.out.println(s.replaceAll(",(?=\\s|$)",""));
Output:
mms§NNP_ACRON site§N_NN pe§PSP ,,,,,§RD_PUNC link§N_NN ....§RD_PUNC CID§NNP_ACRON team§N_NN :)§E
Upvotes: 0
Reputation: 77454
Match the data you want, not the one you don't want.
You probably want ([^ ]+),
and keep the bracketed data, separated by whitespace.
You might even want to narrow it down to ([^ ]+§[^ ]+),
. Usually, stricter is better.
Upvotes: 0
Reputation: 67968
(?<=[^,\s]),
Try this.Replace by empty string
.See demo.
http://regex101.com/r/lZ5mN8/5
Upvotes: 0
Reputation: 37023
You could use ", " as search and replace it with " " (space) as below:
one.replace(", ", " ");
If you think, you have "myString, ,,," or multiple spaces in between, then you could use replace all with regex like
one.replaceAll(",\\s+", " ");
Upvotes: 3