+ Reply to Thread
Results 1 to 2 of 2

Thread: Parsing text via regex

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    34

    Parsing text via regex

    I have a text file and each line contains values Something like : A12101991NameM. The first letter can be either A or P, following numbers correspond to date, next comes name, and then gender(M or F). i`d like to parse that into a map , i really spent time figuring it out but couldn`t . hope you help me and thanks

  2. #2
    The corresponding regex is :
    Code:
    ^(A|P)(\d+)(\w+?)(M|F)$
    But take care if the format is different then you wont get a result :
    Code:
    String str = "A10102012MikeM";
    
    String p = "^([A|P])(\\d+)(\\w+?)(M|F)$";
    Pattern pattern = Pattern.compile(p);
    Matcher matcher = pattern.matcher(str);
    if (matcher.find()){
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
    System.out.println(matcher.group(4));
    }
    output will be : A 10102012 Mike M

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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