Regular expressions: all words after my current on

2019-08-22 06:35发布

I need to remove all strings from my text file, such as:

flickr:user=32jdisffs
flickr:user=acssd
flickr:user=asddsa89

I'm currently using fields[i] = fields[i].replaceAll(" , flickr:user=.*", "");

however the issue with this is approach is that any word after flickr:user= is removed from the content, even after the space.

thanks

3条回答
欢心
2楼-- · 2019-08-22 06:53

Going by the question as stated, chances are that you want:

fields[i] = fields[i].replaceAll(" , flickr:user=[^ ]* ", ""); // or " "

This will match the string, including the value of user up to but not including the first space, followed by a space, and replace it either by a blank string, or a single space. However this will (barring the comma) net you an empty result with the input you showed. Is that really what you want?

I'm also not sure where the " , " at the beginning fits into the example you showed.

The reason for your difficulties is that an unbounded .* will match everything from that point up until the end of the input (even if that amounts to nothing; that's what the * is for). For a line-based regular expression parser, that's to the end of the line.

查看更多
干净又极端
3楼-- · 2019-08-22 06:55

flickr:user=\w+ should do it:

String noFlickerIdsHere = stringWithIds.replaceAll("flickr:user=\\w+", "");

Reference:

\w = A word character: [a-zA-Z_0-9]

查看更多
倾城 Initia
4楼-- · 2019-08-22 07:07

You probably need

replaceAll("flickr:user=[0-9A-Za-z]+", "");

查看更多
登录 后发表回答