Regular expressions: all words after my current one are gone
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(" , fl开发者_运维问答ickr: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
You probably need
replaceAll("flickr:user=[0-9A-Za-z]+", "");
flickr:user=\w+
should do it:
String noFlickerIdsHere = stringWithIds.replaceAll("flickr:user=\\w+", "");
Reference:
\w
= A word character: [a-zA-Z_0-9]
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.
精彩评论