user1328007
user1328007

Reputation: 57

How to remove comma after a word pattern in java

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

Answers (4)

Avinash Raj
Avinash Raj

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

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

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

vks
vks

Reputation: 67968

(?<=[^,\s]),

Try this.Replace by empty string.See demo.

http://regex101.com/r/lZ5mN8/5

Upvotes: 0

SMA
SMA

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

Related Questions