I am trying to come up with a regex for a giftcard number pattern in an application. I have this so far and it works fine:
(?:5049\d{12}|6219\d{12}) = 5049123456789012
What I need to account for though is numbers that are separated by dashed or spaces like so:
5049-1234-5678-9012
5049 1234 5678 9012
Can I chain these patterns together or do I need to make separate for each type?
The easiest and most simple regex could be:
(?:(5049|6219)([ -]?\d{4}){3})
Explanation:
(5049|6219) - Will check for the '5049' or '6219' start
(x){3} - Will repeat the (x) 3 times
[ -]? - Will look for " " or "-", ? accepts it once or 0 times
\d{4} - Will look for a digit 4 times
A more detailed explanation and example can be found here: https://regex101.com/r/A46GJp/1/
Use (?:5049|6219)(?:[ -]?\d{4}){3}
First, match one of the two leads. Then match 3 groups of 4 digits each, each group optionally preceded by space or dash.
See regex101 for demo, and also explains in more detail.
The above regex will also match if separators are mixed, e.g. 5049 1234-5678 9012. If you don't want that, use
(?:5049|6219)([ -]?)\d{4}(?:\1\d{4}){2} regex101
This captures the first separator, if any, and specifies that the following 2 groups must use that same separator.
Try this :
(?:(504|621)9(\d{12}|(\-\d{4}){3}|(\s\d{4}){3}))
https://regex101.com/r/SyjaT5/6
Related
I have been trying to use regular expression to extract data from the following strings
LTE_LTE_FSD9167__P_Airport1
I want to extract the 7 digit sitecode(FSD9167) from the above string.
RUR1251__S_KhooNaiWala
I want to extract 7 digit sitecode(RUR1251) from above string.
For LTE_LTE case I wrote LTE_LTE_([^_;]+).* but it selects the whole string including not the required text only.
The pattern I see is three letters followed by four numbers, so:
\w{3}\d{4}
Use () to capture the pattern:
(\w{3}\d{4})
PHP:
$re = '/(\w{3}\d{4})/m';
JavaScript:
const regex = /(\w{3}\d{4})/gm;
Use https://regex101.com/ to learn the explanation.
You can use something like this:
^(?:LTE_LTE_)?(\S{7})\S*$ /gm
This captures the seven non-whitespace characters either at the beginning (case 2) or just after LTE_LTE_
Demo
You did not provide any rule about how the code could look like. I noticed that both codes you provided in the example have 3 letters followed by 4 digits. I made a rule more generic, with at least 2 letters followed by at least 3 digits.
The regex is:
[a-zA-Z]{2,}\d{3,}
Test here.
As you want to match only these 2 strings, use:
(?<![A-Z0-9])[A-Z0-9]{7}(?![A-Z0-9])
Explanation:
(?<![A-Z0-9]) # negative lookbehind, make sure we haven't alphanum before
[A-Z0-9]{7} # 7 alphanumerics
(?![A-Z0-9]) # negative lookahead, make sure we haven't alphanum after
Demo
I've got the following text:
instance=hostname1, topic="AB_CD_EF_12345_ZY_XW_001_000001"
instance=hostname2, topic="AB_CD_EF_1345_ZY_XW_001_00001"
instance=hostname1, topic="AB_CD_EF_1235_ZY_XW_001_000001"
instance=hostname2, topic="AB_CD_EF_GH_4567_ZY_XW_01_000001"
instance=hostname1, topic="AB_CD_EF_35678_ZY_XW_001_00001"
instance=hostname2, topic="AB_CD_EF_56789_ZY_XW_001_000001"
I would like to capture numbers from the sample above. I've tried to do so with the regular expressions below and they work well as separate queries:
Regex: *.topic="AB_CD_EF_([^_]+).*
Matches: 12345 1345 1235
Regex: *.topic="AB_CD_EF_GH_([^_]+).*
Matches: 4567 35678 56789
But I need a regex which can give me all numbers, ie:
12345 1345 1235 4567 35678 56789
Make GH_ optional:
.*topic="AB_CD_EF_(GH_)?([^_]+).*
which matches all your target numbers.
See live demo.
You could be more general by allowing any number of "letter letter underscore" sequences using:
.*topic="(?:[A-Z]{2}_)+([^_]+).*
See live demo.
Another option that we might call, would be an expression similar to:
topic=".*?[A-Z]_([0-9]+)_.*?"
and our desired digits are in this capturing group ([0-9]+).
Please see the demo for additional explanation.
From the examples and conditions you've given I think you're going to need a very restrictive regex, but this may depend on how you want to adapt it. Take a look at the following regex and read the breakdown for more information on what it does. Use the first group (there is only one in this regex) as a substitution to retrieve the numbers you are looking for.
Regex
^instance\=hostname[0-9]+\,\s*topic\=\“[A-Z_]+([0-9]+)_[A-Z_]+[0-9_]+\”$
Try it out in this DEMO.
Breakdown
^ # Asserts position at start of the line
hostname[0-9]+ # Matches any and all hostname numbers
\s* # Matches whitespace characters (between 0 and unlimited times)
[A-Z_]+ # Matches any upper-case letter or underscore (between 1 and unlimited times)
([0-9]+) # This captures the number you want
$ # Asserts position at end of the line
Although this does answer the question you have asked I fear this might not be exactly what you're looking for but without further information this is the best I can give you. In any case after you've studied the breakdown and played around the demo a it it should prove to be of some help.
The regex worked for me :
/.*topic="(?:[AB_CD_EF_(GH_)]{2,3}_)+([^_]]+).*/
Say i give a pattern 123* or 1234* , i would like to match any 10 digit number that starts with that pattern. It should have exactly 10 digits.
Example:
Pattern : 123 should match 1234567890 but not 12345678
I tried this regex : (^(123)(\d{0,10}))(?(1)\d{10}).. obviously it didn't work. I tried to group the pattern and remaining digits as two different groups. It matches 10 digits after the captured group (https://regex101.com/). How do i check the captured group is exactly 10 digits? Or is there any good knacks here. Please guide me.
Sounds like a case for the positive lookahead:
(?=123)\d{10}
This will match any sequence of exactly 10 digits but only if prefixed with 123. Test it here.
Similarly for prefix 1234:
(?=1234)\d{10}
Of course, if you know the prefix length upfront, you can use 123\d{7}, but then you'll have to change range limits with each prefix change (for example: 1234\d{6}).
Additionally, to ensure only isolated groups of 10 digits are captured, you might want to anchor the above expression with a (zero-length) word boundary \b:
\b(?=123)\d{10}\b
or, if your sequence can appear inside of the word, you might want to use negative lookbehind and lookahead on \d (as suggested in comments by #Wiktor):
(?<!\d)(?=123)\d{10}(?!\d)
I would keep it simple:
import re
text = "1234567890"
match = re.search("^123\d{7}$|^1111\d{6}$", text)
if match:
print ("matched")
Just throw your 2 patterns in as such and it should be good to go! Note that 123* would catch 1234* so I'm using 1111\d{6} as an example
I am trying to create a regex to validate a field where the user can enter a 5 digit number with the option of adding a / followed by 3 letters. I have tried quite a few variations of the following code:
^(\d{5})+?([/]+[A-Z]{1,3})?
But I just can't seem to get what I want.
For instance l would like the user to either enter a 5 digit number such as 12345 with the option of adding a forward slash followed by any 3 letters such as 12345/WFE.
You probably want:
^\d{5}(?:/[A-Z]{3})?$
You might have to escape that forward slash depending on your regex flavor.
Explanation:
^ - start of string anchor
\d{5} - 5 digits
(?:/[A-Z]{3}) - non-capturing group consisting of a literal / followed by 3 uppercase letters (depending on your needs you could consider making this a capturing group by removing the ?:).
? - 0 or 1 of what precedes (in this case that's the non-capturing group directly above).
$ - end of string anchor
All in all, the regex looks like this:
You can use this regex
/^\d{5}(?:\/[a-zA-Z]{3})?$/
^\d{5}(?:/[A-Z]{3})?$
Here it is in practice (this is a great site to test your regexes):
http://regexr.com?36h9m
^(\d{5})(\/[A-Z]{3})?
Tested in rubular
I have a series of 8-digit-numbers that I need to capture via RegEx.
Single whitespaces can occur before, after and in some cases between the digits. In some cases, other chars follow. Here's the most common variations, each of which I want to capture as 12345678:
123456789
12345678
1234567 89S
12345 678 9
123 456789
123456 789
Is this possible?
I think a regex like:
(( )?\d){8}
Would suffice to capture the digits - I'd then remove the whitespace (before further processing) as a separate step.
I'm not sure how strictly to interpret the OP's "single whitespaces" requirement, but it's why I've structured my RegEx to accept 8 digits, each of which is optionally prefixed by a single space character.
If it should only match if there are single spaces, and not any more, the above works whereas the "strip whitespace first" or "strip non-digits first" approaches will not.
If more spaces are allowed, it's easy to change the ? to a * or any fixed upper limit.
This is not possible in a single "regex" step. I can go into more detail if you like, but basically regex cannot "count" (it can only match a specified match size, such as "8 numbers", but not "an unknown number of characters, 8 of which are numbers").
You need to do this in two stages -
first remove whitespace.
then perform a regex match.
For instance, in ruby:
thingtomatch = " 12 3456 7899X"
temp = thingtomatch.squeeze(' ').strip # => temp="1234567899X"
matched_digits = temp.match(/(\d{8}).*/)[1]
(Or, as other answers have suggested, you could perform a regex match and then remove whitespace from the result.)
You can do it, but in two steps:
First, remove non-digits:
s/[^\d]//g
Second, match your digits:
m/^(\d{8})$/