Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: regular expressions, characters unallowed in file names

  1. #1
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 "://"
    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");
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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):

    int counter = 0;
    while(matcher.find(counter)
    {
       filename = matcher.group();
       counter = matcher.end();
       System.out.println("\n" + filename + "\n");
    }

  3. #3
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)
    Last edited by chopficaro; May 6th, 2010 at 11:21 AM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: December 22nd, 2011, 08:46 AM
  2. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM
  3. regular expressions
    By brad35309 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2010, 08:30 PM
  4. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM
  5. Text Processing with Regular Expressions explained in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 6th, 2008, 02:03 AM