I have this regex
"((\\s{0,2})\\p{XDigit}{2}\\s{0,2})*"
the user can select the matching string from Byte dump like this

the selection above should be possible but selecting half of a Byte shouldn´t like this

spaces at the end or beginning shouldn´t be a problem like this

the problem with the given regex is that it takes far too long to match. what can i improve, what is the problem?
edit:
so i build a solution for this case. the only thing i need to check ist the beginning and the end of the string. removing the spaces and check if the first and last elements length of the splitted string is 1. I am Splitting it anyway because after that i am parsing it to a byte Array.
String selection = dumpText.getSelectionText();
if (selection.equals(" ") || selection.equals(" ")){
return;
}
//remove spaces at the beginning
while(selection.charAt(0) == ' '){
selection = selection.substring(1);
}
//remove spaces at the end
while(selection.charAt(selection.length()-1) == ' '){
selection = selection.substring(0, selection.length()-1);
}
String[] splitted = selection.split("\\s{1,2}");
if(splitted.length == 0 || splitted[0].length()==1 || splitted[splitted.length-1].length()==1){
return;
}
When you are asking something simple, a basic string comparison will be more efficient. In this case you are only interested in the first 2 and last 2 characters.
So you could test only those (after validating the length):
s.charAt(0) != ' ' && s.charAt(1) == ' '
&& s.charAt(s.length - 1) != ' ' && s.charAt(s.length - 2) == ' '
Although this isn't as fancy, it will be very fast. You just test if you have a single character and then a space, the other way around at the end.
This only works for basic validation though.
Try this pattern:
\s{0,2}(?:\p{XDigit}{2}\s{0,2})*
You're experiencing Catastrophic Backtracking, where (in this case) you have multiple ways of failing to match the string.
The pattern as I've written is is basically the same, but should have only one way of matching the selection:
\s{0,2} - Optional leading spaces(?:\p{XDigit}{2}\s{0,2})* - one or more hexadecimal pairs, with spaces after it.Note that it is possible for this pattern to match hexadecimal digits without spaces, like 12AB, but it should work for your use case anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With