简体   繁体   中英

Count & Split by regex pattern in java

I have a string in below format.

-52/ABC/35/BY/200/L/DEF/307/C/110/L

I need to perform the following.

 1. Find the no of occurrences of 3 letter word's like ABC,DEF in the above text.
 2. Split the above string by ABC and DEF as shown below. 
    ABC/35/BY/200/L
    DEF/307/C/110/L

I have tried using regex with below code, but it always shows the match count is zero. How to approach this easily.

static String DEST_STRING = "^[A-Z]{3}$";
    static Pattern DEST_PATTERN = Pattern.compile(DEST_STRING,
            Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    public static void main(String[] args) {
        String test = "-52/ABC/35/BY/200/L/DEF/307/C/110/L";
        Matcher destMatcher = DEST_PATTERN.matcher(test);
        int destCount = 0;
        while (destMatcher.find()) {
            destCount++;
        }
        System.out.println(destCount);
    }

Please note i need to use JDK 6 for this,

You can use this code :

public static void main(String[] args) throws Exception {
    String s = "-52/ABC/35/BY/200/L/DEF/307/C/110/L";
    // Pattern to find all 3 letter words . The \\b means "word boundary", which ensures that the words are of length 3 only. 
    Pattern p = Pattern.compile("(\\b[a-zA-Z]{3}\\b)");
    Matcher m = p.matcher(s);
    Map<String, Integer> countMap = new HashMap<>();
    // COunt how many times each 3 letter word is used.
   // Find each 3 letter word.
    while (m.find()) {
        // Get the 3 letter word.
        String val = m.group();
        // If the word is present in the map, get old count and add 1, else add new entry in map and set count to 1
        if (countMap.containsKey(val)) {
            countMap.put(val, countMap.get(val) + 1);
        } else {
            countMap.put(val, 1);
        }
    }
    System.out.println(countMap);
    // Get ABC.. and DEF.. using positive lookahead for a 3 letter word or end of String 
    // Finds and selects everything starting from a 3 letter word until another 3 letter word is found or until string end is found.
    p = Pattern.compile("(\\b[a-zA-Z]{3}\\b.*?)(?=/[A-Za-z]{3}|$)");
    m = p.matcher(s);
    while (m.find()) {
        String val = m.group();
        System.out.println(val);
    }

}

O/P :

{ABC=1, DEF=1}
ABC/35/BY/200/L
DEF/307/C/110/L

Check this one:

String stringToSearch = "-52/ABC/35/BY/200/L/DEF/307/C/110/L";
Pattern p1 = Pattern.compile("\\b[a-zA-Z]{3}\\b");
Matcher m = p1.matcher(stringToSearch);

int startIndex = -1;
while (m.find())
{
    //Try to use Apache Commons' StringUtils
    int count = StringUtils.countMatches(stringToSearch, m.group());   
    System.out.println(m.group +":"+ count);

    if(startIndex != -1){
      System.out.println(stringToSearch.substring(startIndex,m.start()-1));
    }
    startIndex = m.start(); 
}
if(startIndex != -1){
    System.out.println(stringToSearch.substring(startIndex));
}

output:

ABC : 1

ABC/35/BY/200/L

DEF : 1

DEF/307/C/110/L

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM