How can i write this as a regular expression?
“blocka#123#456”
i have used # symbol to split the parameters in the data
and the parameters are block name,startX coordinate,start Y corrdinate
this is the data embedded in my QR code.so when i scan the QR i want to check if its the right QR they’re scanning. For that i need a regular expression for the above syntax.
my method body
public void Store_QR(String qr){
if( qr.matches(regular Expression here)) {
CurrentLocation = qr;
}
else // Break the operation
}
myregexp.com is nice to do some testing.
Official Java Regex Tutorial is quite ok to learn and includes most things one needs to know.
The Pattern documentation also includes fancy predefined character classes that are missing in above tutorial.
You did not specify anything that has to be regular in that example you gave. Regular expressions make only sense if there are rules to validate the input.
If it has to be exactly
"blocka#123#456"then"blocka#123#456"or"^blocka#123#456$"will work as regex. Stuff between^and$means that the regex inside must span from begin to end of the input. Sometimes required and usually a good idea to put that around your regex.If
blockais dynamic replace it with[a-z]+to match any sequence of lowercase lettersathroughzwith length of at least 1.block[a-z]would matchblocka,blockb, etc.And
[a-z]{6}would match any sequence of exactly 6 letters.[a-zA-Z]also includes uppercase letters and\p{L}matches any letter including unicode stuff (e.g.Blüc本).#matches#. Like any character without special regex meaning (\ ^ $ . | ? * + ( ) [ ] { }) characters match themselves.[^#]matches every character but#.Regarding the numbers:
[0-9]+or\d+is a generic pattern for several numbers,[0-9]{1,4}would match anything consisting out of 1-4 numbers like007,5,9999.(?:0|[1-9][0-9]{0,3})for example will only match numbers between0and9999and does not allow leading zeros.(?:STUFF)is a non-capturing group that does not affect the groups you can extract viaMatcher#group(1..?). Useful for logical grouping with|. The meaning of(?:0|[1-9][0-9]{0,3})is: either a single0OR ( 1x1–9followed by 0 to 3 x0–9).[0-9]is so common that there is a predefinition for it :\d(digit). It’s\\dinside the regexStringsince you have to escape the\.So some of your options are
".*"which matches absolutely everything"^[^#]+(?:#[^#]+)+$"which matches anything separated by#like"hello #world!1# -12.f #本#foo#bar""^blocka(#\\d+)+$"which matchesblockafollowed by at least one group of numbers separated by#e.g.blocka#1#12#0007#949432149#3"^blocka#(?:[0-9]|[1-9][0-9]|[1-3][0-9]{2})#[4-9][0-9]{2}$"which will match only if it findsblocka#followed by numbers 0 – 399, followed by a#and finally numbers 400-999"^blocka#123#456$"which matches only exactly that string.All that are regular expressions that match the example you gave.
But it’s probably as simple as
or
I guess a good regex for that would be
"^block\\w+#\\d+#\\d+$", starting with"block", then any combination ofa–z,A–Z,0–9and_(thats the\w) followed by#, numbers,#, numbers.Would match
block_#0#0,blockZ#9#9,block_a_Unicorn666#0000#1234, but notblock#1#2because there is no name at all and would not matchblockName#123#abcbecause letters instead of number. Would also not matchBlock_a#123#456because of the uppercase B.If the name part (
\\w+) is too liberal (___,_123would be a legal names) use e.g."^block_?[a-zA-Z]+#\\d+#\\d+$", what won’t allow numbers and names may only be separated by a single optional_and there have to be letters after that. Would allow_a,a,_ABc, but not_,_a_b,_a9. If you want to allow numbers in names[a-zA-Z0-9]would be the character class to use.