Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Not Matching Correctly

Tags:

java

regex

Hi I have to match a pattern like below

{digit 0-1 or A}:{digit 0-1 or A}:{digit 0-1 or A}|{digit 0-1 or A}:{digit 0-1 or A}:{digit 0-1 or A}|{digit 0-1 or A}:{digit 0-1 or A}:{digit 0-1 or A}

I am using the following code -

String accMatrixPattern = "\\d{1,1}|[A]:\\d{1,1}|[A]:\\d{1,1}|[A]|[A]:\\d{1,1}|[A]"; 
String accMatrx = "1:A:1|0:1:1|0:1:1";

If I am using only "\\d{1,1}|[A]"; it is working but not combined.

Please suggest How to match Regular Expression

Thanks

like image 796
Kumar Avatar asked Apr 25 '26 22:04

Kumar


1 Answers

If you're trying to match just 0, 1 or A in each position, you can use:

String accMatrixPattern = "[01A]:[01A]:[01A]\\|[01A]:[01A]:[01A]\\|[01A]:[01A]:[01A]";

If you want to take values, -1, 0, 1, A:

String accMatrixPattern = "([01A]|-1):([01A]|-1):([01A]|-1)\\|([01A]|-1):([01A]|-1):([01A]|-1)\\|([01A]|-1):([01A]|-1):([01A]|-1)";
like image 56
Szymon Avatar answered Apr 27 '26 10:04

Szymon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!