Regex Generation Assistance - regex

I need some hopefully easy and quick help. I am getting a string of data from a barcode and I need to splice it to obtain three different values - Account Number, Date, and Invoice Number.
The regex has to be written for .Net Framework.
An example of what the barcode will return looks like this –
000295934009000306511302017001
The first 9 digits is the Account Number. I’ve got that figured out by using this - ^\d{9}
The next 10 digits is the Invoice Number. I am not sure what regex expression would give me that.
Then, the 8 digits following the Invoice Number is the date (in this example it is 11302017). I need to pull that and put it into a MM/DD/YYYY format.
I will be creating a named group pattern for this too.
(?<account>^\d{9})(?<date>XXXXXXX)(?<invoice>XXXXXXX) - the X's are what I need to figure out.
Any help is greatly appreciated!

This is the regex:
(\d{9})(\d{10})(\d{8})
Self explanatory: group 1 9 digits, group 2 10 digits and group 3 8 digits.
You can use it like this:
string pattern = #"(\d{9})(\d{10})(\d{8})";
string input = #"000295934009000306511302017001";
var match = Regex.Match(input, pattern);
var accountNumber = match.Groups[1].Value;
var invoiceNumber = match.Groups[2].Value;
var date = match.Groups[3].Value;
To get the DateTime object, use ParseExact:
DateTime.ParseExact(date, "MMddyyyy", CultureInfo.InvariantCulture)
Remember to add using directives for System.Text.RegularExpressions and System.Globalization!

This is the regex:
(\d{9})(\d{10})([0-1][0-2][0-3][0-9]\d{4})
accountNumber - Group1
invoiceNumber - Group2
date - Groups3

Related

Regex Replace First Number from Phone number if it is a 1

I've got a list of phone numbers in bigquery.
Some have the number 1 in front and some do not. I would like to remove the 1s using regex replace:
The data looks as follows:
16047779887
4037778776
And I would like to return:
6047779887
4037778776
Any help is appreciated!
Select regex_replace(column_name, "1*", "")
from table
The * represents the rest of the string.
If the first letter is 1, remove it. (replace it with an empty string)

Reg exp search in notes/comments/description data in PostgreSQL 10.7

I have a scenario which I am not able to do in 10.7 version. Basically, I have a data column in which I need to find the Reg Exp pattern inside the data which is in the form of notes/comments/description.
For example, Data in the column : The SSN number is 760-56-6289
In the above data 760-56-6289 is the actual SSN number which I need to find across all schemas/tables/columns for the defined reg exp pattern. And, we can have a pre or post text for actual SSN value.
Could you please let me know how to achieve this PostgreSQL 10.7?
Please let me know if you need more information for the same.
demo:db<>fiddle
SELECT
(regexp_matches(mycolumn, '^.*([\d]{3}-[\d]{2}-[\d]{4}).*$'))[1]
FROM mytable
The RegEx means:
Start of text: ^
arbitrary number of characters: .*
group of your number: (...)
3 digit characters: [\d]{3}
- character
2 digits: [\d]{2}
- character
4 digits: [\d]{4}
arbitrary number of characters: .*
end of text: $
regexp_matches() gives out all found groups as an array. So, there is only one group, the array contains only one value. This is your number which can be get with the index [1]

Regex Get Number In Between Word & Digits

I have the following string:
[Tag|String|WORD012311120151218]
How can I get 0123111 from WORD012311120151218 - the problem I'm having is that 0123111 is dynamic and can come through the system as any length.
My thought process is as follows but I can't seem to get it right after searching: extract any value from after WORD and ends before the date 20151218 - the date changes daily but it's always going to be 8 digits.
Is this possible?
You can use this regex:
.*WORD(\d+)\d{8}\].*
And get the group $1.
Explanation in this link.
In Java, it would be:
String input = "[Tag|String|WORD012311120151218]";
String num = input.replaceAll(".*WORD(\\d+)\\d{8}\\].*", "$1");
System.out.println(num); //prints 0123111
/\[.*WORD([0-9]+)[0-9]{8,8}\]$/
should do it, with suitable escapes depending on which regex flavor you're using. ([0-9]+) is what you want, and [0-9]{8,8} is 8 trailing digits for the date.
'WORD012311120151218'.match(/(\d+)(\d{8}$)/)[1]

Date format comparmission

I have question. I am trying to prepare date regex comparmission. The problem is month and day if its one digit it can be present as 03 or 3 for both month and day. For instance possible values:
2015/03/27 or 2015/4/12 or 2015/07/05 or 2015/2/2 or 2015/02/3
What i did so far is:
^(?<Month>\d(0([0-1]|1[0-2])|([1-12])){1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))$
I started to make now for month:
(?<Month>\d(0([0-1]|1[0-2])|([1-12])){1,2})
(0([0-1]|1[0-2])|([1-12])){1,2})
so {1,2} - because can be one digit or two for instance (12, 2, 02)
0([0-1]|1[0-2]) | ([1-12])) - because can be two digits or one
somehow i cant figure it into the final version.
Can you help me out?
Using just \d, you might end up with fake dates, like 12/67/4567.
Also, your input has another date format: Year/Month/Day.
I suggest using this regex for your input format:
^(?<Year>(?:19|20)\d{2})\/(?<Month>0?[1-9]|1[0-2])\/(?<Day>3[01]|0?[1-9]|[12][0-9])$
See demo
Optional 0s are made possible due to the ? quantifier after 0.
If it is for .NET, you do not have to escape /s.
To validate the date, use the classes and methods of the programming environment you are using. Here is an example in C#:
var resultFromRegex = "2015/03/27";
DateTime validDate;
var isValid = DateTime.TryParseExact(resultFromRegex, "yyyy/MM/dd", new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.None, out validDate);

REGEX : Extract group of number where digits are more than 3

HI I have a question regarding REGEX.
This sounds very simple and I remember doing it but somehow it got deleted and I am finding it hard to get it back.
I want to extract group of numbers from one line.
If the count of digits > 3 - select that.
EG:
ga3rdparty/phpMyAdmin/i0ndex.php?&t0oken=abf540063shakk
This line can be different everytime but there will be only 1 group of digits with more than 2 digits.
OUTPUT: 540063
Thank you in advance
You can use \d{3,} where 3 is the minimum number of digits. You an take a look at the following python code
import re
var= "ga3rdparty/phpMyAdmin/i0ndex.php?&t0oken=abf540063shakk"
pattern = re.compile(r'\d{3,}')
for match in pattern.findall(ver):
print(match)