Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: Regex joy

  1. #1
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Regex joy

    I don't often ask questions here, but this one (especially being a regex thing) has me a bit stumped.

    I'm trying to piece together a regex that allows characters, digits and a couple of other characters.
    Essentially:
    ^[a-zA-Z0-9:,';\/]+$

    Also it can't start or end with a slash...so the above becomes:
    ^[^\/][a-zA-Z0-9:,';\/]+[^\/]$

    So this gives a match:
    abcD,xy/z
    and this doesn't
    /abcD,xy/z

    Hurrah!

    However, it is supposed to allow &amp and &quot...like that.

    I got this far:
    ^[^\/](&amp|&quot|[a-zA-Z0-9:,';\/])+[^\/]$

    which allows this:
    &ampaB,c/def;g&ampi.J&quote'q
    but not this:
    &ampaB,c/def;g&ampi.J&quote'&quot

    If either &amp or &quot are at the end it fails...and I don't know why or how to get it to behave.
    Or even if this is something I ought to be doing as a regex.

    So...any ideas?

    (Yes, I know this isn't Java as such, but it is for a validator written in Java)
    Last edited by Tolls; 04-24-2017 at 12:54 PM. Reason: Fix my allows and not allows strings
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Regex joy

    Took the liberty to take a stab at it. The things I changed:
    - Added the literal period as character since it occurs in the second example set
    - Added quantifiers to the &amp, &quot and character classes
    Java Code:
    import java.util.regex.Pattern;
    
    class Tolls {
        static Pattern pattern = Pattern.compile("^[^\\/](&amp*|&quot*|[\\.a-zA-Z0-9:,';\\/]+)+[^\\/]$");
    
        public static void main(String[] args) {
            test("abcD,xy/z");
            test("/abcD,xy/z");
            test("&ampaB,c/def;g&ampi.J&quote'q");
            test("&ampaB,c/def;g&ampi.J&quote'&quo");
            test("&ampaB,c/def;g&ampi.J&quote'&quot");
            test("&ampaB,c/def;g&ampi.J&quote'&quot/");
        }
    
        private static void test(String text) {
            System.out.printf("text = %s, matches = %s%n", text, pattern.matcher(text).matches());
        }
    
    }

    Result:
    Java Code:
    text = abcD,xy/z, matches = true
    text = /abcD,xy/z, matches = false
    text = &ampaB,c/def;g&ampi.J&quote'q, matches = true
    text = &ampaB,c/def;g&ampi.J&quote'&quo, matches = false
    text = &ampaB,c/def;g&ampi.J&quote'&quot, matches = true
    text = &ampaB,c/def;g&ampi.J&quote'&quot/, matches = false
    Last edited by SurfMan; 04-24-2017 at 02:09 PM.
    Tolls likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Regex joy

    Regex joy-regex.jpg
    Tolls likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Regex joy

    Ah, I cut down the original one (there's more special characters), but clearly forgot to strip out the '.'!

    And you beauty...so, what do the '*' actually mean?
    I don't think I've used those before.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: Regex joy

    The * is zero or more
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Regex joy

    Ta!

    All green on the tests.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Regex joy

    When I first read this I kept thinking what's that large V in the expression? Then I realized it was opposing slashes. Time for more coffee.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. RegEx
    By watle in forum New To Java
    Replies: 0
    Last Post: 03-22-2011, 08:07 AM
  2. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  3. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  4. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM
  5. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •