Search code examples
regexregex-groupregex-greedy

Regex for extracting the exception names


I want to extract the exception name from the below sentences using regex pattern,

  1. Error: MYTERA RuntimeException: No task output
  2. Error: android.java.lang.NullPointerException.checked

I need the terms RuntimeException and NullPointerException with a single Regex pattern.


Solution

  • This expression might help you to do so:

    ([A-Za-z]+Exception)
    

    enter image description here

    Graph

    This graph shows how the expression would work and you can visualize your expressions in this link:

    enter image description here

    Performance

    This JavaScript snippet shows the performance of that expression using a simple 1-million times for loop.

    repeat = 1000000;
    start = Date.now();
    
    for (var i = repeat; i >= 0; i--) {
    	var string = 'Error: android.java.lang.NullPointerException.checked';
    	var regex = /(.*)\.([A-Za-z]+Exception)(.*)/g;
    	var match = string.replace(regex, "$2");
    }
    
    end = Date.now() - start;
    console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
    console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");