regular expressions, characters unallowed in file names
trying to take a url and turn it into a file name by grabbing the last bit of the string that doesnt have any characters that arent allowed in file names, but this keeps returning the same sting over and over again "://"
Code :
System.out.println("\n" + filename + "\n");
Pattern pattern = Pattern.compile("[\\\\/:\\*\\?\\\"<>\\|][^[\\\\/:\\*\\?\\\"<>\\|]]+");
Matcher matcher = pattern.matcher(filename[i]);
while (matcher.find())
{
filename = matcher.group();
System.out.println("\n" + filename + "\n");
}
Re: regular expressions, characters unallowed in file names
Everytime you call matcher.find(), it will look starting from the beginning, so the first item it finds is going to be returned every time (in this case, "://")
To get it to look at a different location, you can give the find() method an offset to look (say, the end of the last group it found):
Code :
int counter = 0;
while(matcher.find(counter)
{
filename = matcher.group();
counter = matcher.end();
System.out.println("\n" + filename + "\n");
}
Re: regular expressions, characters unallowed in file names
that doesnt seem to be working, and this code prooves that the matcher.find() continues from the last find without the use of the counter
Test Harness (The Java™ Tutorials > Essential Classes > Regular Expressions)
Re: regular expressions, characters unallowed in file names
::blush:: my bad. I was thinking of using multiple regex's, but yes, using one regex you don't need to.
The problem I think is that you're matcher is finding everything that's invalid and only returning those. There are two solutions:
1. Change your regex to find only things that are valid, and just keep searching for the last valid item.
2. Use your method and keep track of the index of the last invalid item. Then use the substring() method to get everything after the index.