What does the new BCH regex mean? - regex

BCH regex was recently updated (in the API) to: "address_regex": "^([13][a-km-zA-HJ-NP-Z1-9]{25,34})|^((bitcoincash:)?(q|p)[a-z0-9]{41})|^((BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$"
Is this a Segwit thing?
I understand it's now saying addresses may start with "bitcoincash:" or "BITCOINCASH:", but that's a thing, or is it some internal Coinbase designation?

Breaking down this regex, there are three possible that constitute a valid BCH address:
1st Alternative ^([13][a-km-zA-HJ-NP-Z1-9]{25,34}):
Starts with either a 1 or a 3
Follows this with between 25 and 34 alphanumeric characters excluding l, I, O and 0
2nd Alternative ^((bitcoincash:)?(q|p)[a-z0-9]{41}):
Starts with the literal string bitcoincash: (strangely this can occur more than once)
Follows this with either a q or a p
Follows this with exactly 41 alphanumeric characters (only in lowercase)
3rd Alternative ^((BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$:
Starts with the literal string BITCOINCASH: (strangely this can occur more than once)
Follows this with either a Q or a P
Follows this with exactly 41 alphanumeric characters (only in uppercase)
Essentially, Coinbase is now simply accepting the three above regexes as valid BCH addresses, adding bitcoincash as a recognised protocol used by BCH.

let me break it down for you
so there are three regex in it, as after new additions now all three are accepted as valid BCH addresses now
/^([13]{1}[a-km-zA-HJ-NP-Z1-9]{33}|(bitcoincash:)?(q|p)[a-z0-9]{41}|(BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$/
Breaking it down
First type of addresses
[13]{1}
address will start with L, M or 3, {1} defines that only match one character in square bracket
/[13]{1}[a-km-zA-HJ-NP-Z1-9]/
cannot have l (small el), I (capital eye), O (capital O) and 0 (zero)
/[13]{1}[a-km-zA-HJ-NP-Z1-9]{26,33}/
can be 27 to 34 characters long, remember we already checked the first character to be 1 or 3, so remaining address will be 26 to 33 characters long
second type of address
bitcoincash:
will start with bitcoincash:
(bitcoincash:)?(q|p)
followed by q or p
(bitcoincash:)?(q|p)[a-z0-9]
can only have lower case letters and numbers
(bitcoincash:)?(q|p)[a-z0-9]{41}
will be 54 characters long, we already checked first 11 characters to be bitcoincash: followed by another character to be Q or p, so remaining address will be 41 characters long
third type of address
BITCOINCASH:
will start with BITCOINCASH:
(BITCOINCASH:)?(Q|P)
followed by Q or P
(BITCOINCASH:)?(Q|P)[a-z0-9]
can only have lower case letters and numbers
(BITCOINCASH:)?(Q|P)[a-z0-9]{41}
will be 54 characters long, we already checked first 11 characters to be BITCOINCASH: followed by another character to be Q or P, so remaining address will be 41 characters long

Related

Regular Expression

I'm trying to get the regular expression to work (using jQuery) for a specific pattern I need.
I need following pattern:
First two character
s of the string need to be numbers (0-9) but maximum number is 53. for numbers below 10 a leading 0 is required
Character on position 3 needs to be a .
the next 4 characters need to be a number between 0-9, minimum number should be 2010, maximum 2050
so, Strings like 01.2020, 21.2020, or 45.2020 have to match but 54.2020 or 04.2051 must not.
I tried to write the regex without the min and max requirement first and I'm testing the string using regex101.com but I'm unable to get it to work.
acording to the definition /^[0-9]{2}\.\d[0-9]{4}$/ should allow me to insert the strings in the format NN.NNNN.
thankful for any input.
2 numbers from 00 to 53 can be matched using this : (?:[0-4][0-9]|5[0-3]) (00 -> 49 or 50 -> 53)
Character on position 3 needs to be a . : you've already got the \.
a number between 2010 and 2050 -> 20(?:[1-4][0-9]|50) (20 followed by either 10 -> 49 or 50)
This gives :
(?:[0-4][0-9]|5[0-3])\.20(?:[1-4][0-9]|50)

regex pattern allowing decimal after 4 digit

I need help in creating a regex pattern which allows '.' after every 4 digits and length should not be greater than 11. eg.
1234.5678 is valid
12345 is invalid
1234.5678.9 is valid
1234.5678.91 is invalid as the length of a string is greater than 11
Thanks
Why don't just combine with | all (three only) possible cases?
^[0-9]{1,4}$ - no dot
^[0-9]{4}\.[0-9]{1,4}$ - one dot
^[0-9]{4}\.[0-9]{4}\.[0-9]{1,2}$ - two dots
so the final pattern will be
(^[0-9]{1,4}$)|(^[0-9]{4}\.[0-9]{1,4}$)|(^[0-9]{4}\.[0-9]{4}\.[0-9]{1,2}$)
In the answer I've suggested (since you've not provided the samples) that
Empty string ("") is not valid
Short numbers (e.g. "123", "1234") are valid
Dangling dots (e.g. "1234.", "1234.5678.") are invalid

Regular expression for A123ABC

I have a string in the format A123ABC
First letter cannot contain <I,O,Q,U,Z>
Next 3 digits (0-9) from 21-998
Last 3 letters cannot include <I,Q,Z>
I used the following expression [A-HJ-NPR-TV-Y]{1}[0-9]{2,3}[A-HJ-PR-Y]{3}
But I am not able to restrict the number in the range 21-998.
Your letter part is fine, below is just the numbers portion:
regex = "(?:2[1-9]|[3-9][0-9]|[1-8][0-9][0-9]|9[0-8][0-9]|99[0-8])"
(?:...) group, but do not capture.
2[1-9] covers 21-29
[3-9][0-9] covers 30-99
[1-8][0-9][0-9] covers 100-899
9[0-8][0-9] covers 900-989
99[0-8] covers 990-998
| stands for "or"
Note: [0-9] may be replaced by \d. So, a more concise representation would be:
regex = "(?:2\d|[3-9]\d|[1-8]\d{2}|9[0-8]\d|99[0-8])"
One option would be matching (\d+) and checking if that falls in the range 21 - 998 outside a regex, in the language you're using, if possible.
If that is not feasible, you have to break it up (just showing the middle part):
(2[1-9]|[3-9]\d|[1-8]\d\d|9[0-8]\d|99[0-8])
Breakdown:
2[1-9] matches 21 - 29
[3-9]\d matches 30 - 99
[1-8]\d\d matches 100 - 899
9[0-8]\d matches 900 - 989
99[0-8] matches 990 - 998
Also, the {1} is superfluous and can be omitted, making the complete regex
[A-HJ-NPR-TV-Y](2[1-9]|[3-9]\d|[1-8]\d\d|9[0-8]\d|99[0-8])[A-HJ-PR-Y]{3}
Assuming the numbers between 21 and 99 are displayed with three digits (ie. : 021, 055, 099), here's a solution for the number part :
((02[1-9])|(0[3-9][0-9])|([1-8][0-9]{2})|(9([0-8][0-9])|(9[0-8])))
Entire regex :
[A-HJ-NPR-TV-Y]{1}((02[1-9])|(0[3-9][0-9])|([1-8][0-9]{2})|(9([0-8][0-9])|(9[0-8])))[A-HJ-PR-Y]{3}
There are probably easier ways to do this, but one way would be to use:
^((?=[^IOQUZ])([A-Z]))((02[^0])|(0[3-9]\d)|([1-8]\d\d)|(9[0-8]\d)|(99[0-8]))((?=[^IQZ])([A-Z])){3}$
To explain:
^ denotes the beginning of the string.
((?=[^IOQUZ])([A-Z])) would give you any capital letter not in <I, O, Q, U, Z>.
((02[^0])|(0[3-9]\d)|([1-8]\d\d)|(9[0-8]\d)|(99[0-8])) denotes any number between ((21 to 29) or (30 to 99) or (100 to 899) or (900 to 989) or (990 to 998)).
((?=[^IQZ])([A-Z])){3} would match any three capital letters not in <I, Q, Z>.
$ would denote the end of the string.

Return some middle portion if length is over x?

I'm currently using a RegEx like so (.{12}$)|(^.{1,12}$) - something like "if the value is 1 to 12 long, return them, else return the last 12."
I'm looking to add another clause, where if the length is greater than 16, I need to return a subset of it (specifically, starting at the 5th character, return 12).
How can I do this within the same RegEx?
It seems to work with
(?<=^.{4}).{12}|^.{1,12}$
This contains two parts. The first one handles strings at least 16 characters in length. For that it starts matching at the fifth character (that's what the (?<=^.{4}) is for – ensuring that there are exactly four characters preceding). After that there have to be at least 12 characters which are matched.
The second part is just for strings with 1–12 characters, matching the complete string.
Quick PowerShell test:
PS> '12345','1234567890','abcdefghijklmnopqrst'|%{if($_-match'(?<=^.{4}).{12}|^.{1,12}$'){$Matches}}
Name Value
---- -----
0 12345
0 1234567890
0 efghijklmnop

Reg-ex, Find x then N characters if N+1 == x

OK here is what I have:
(24(?:(?!24).)*)
its works in the fact it finds from 24 till the next 24 but not the 2nd 24... (wow some logic).
like this:
23252882240013152986400000006090000000787865670000004524232528822400513152986240013152986543530000452400
it finds from the 1st 24 till the next 24 but does not include it, so the strings it finds are:
23252882 - 2400131529864000000060900000007878656700000045 - 2423252882 - 2400513152986 - 24001315298654353000045 - 2400
that is half of what I want it to do, what I need it to find is this:
23252882 - 2400131529864000000060900000007878656700000045 - 2423252882240051315298624001315298654353000045 - 2400
lets say:
x = 24
n = 46
I need to:
find x then n characters if the n+1 character == x
so find the start take then next 46, and the 45th must be the start of the next string, including all 24's in that string.
hope this is clear.
Thanks in advance.
EDIT
answer = 24.{44}(?=24)
You're almost there.
First, find x (24):
24
Then, find n=46 characters, where the 46 includes the original 24 (hence 44 left):
.{44}
The following character must be x (24):
(?=24)
All together:
24.{44}(?=24)
You can play around with it here.
In terms of constructing such a regex from a given x, n, your regex consists of
x.{n-number_of_characters(x)}(?=x)
where you substitute in x as-is and calculate n-number_of_characters(x).
Try this:
(?(?=24)(.{46})|(.{25})(.{24}))
Explanation:
<!--
(?(?=24)(.{46})|(.{25})(.{24}))
Options: case insensitive; ^ and $ match at line breaks
Do a test and then proceed with one of two options depending on the result of the text «(?(?=24)(.{46})|(.{25})(.{24}))»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=24)»
Match the characters “24” literally «24»
If the test succeeded, match the regular expression below «(.{46})»
Match the regular expression below and capture its match into backreference number 1 «(.{46})»
Match any single character that is not a line break character «.{46}»
Exactly 46 times «{46}»
If the test failed, match the regular expression below if the test succeeded «(.{25})(.{24})»
Match the regular expression below and capture its match into backreference number 2 «(.{25})»
Match any single character that is not a line break character «.{25}»
Exactly 25 times «{25}»
Match the regular expression below and capture its match into backreference number 3 «(.{24})»
Match any single character that is not a line break character «.{24}»
Exactly 24 times «{24}»
-->