I need to check if a string begins with 2 specific letters and then is followed by any 4 numbers.
the 2 letters are "BR" so BR1234 would be valid so would BR7412 for example.
what bit of code do I need to check that the string is a match with the Regex in C#?
the regex I have written is below, there is probably a more efficient way of writing this (I'm new to RegEx)
[B][R][0-9][0-9][0-9][0-9]
You can use this:
Regex regex = new Regex(#"^BR\d{4}");
^ defines the start of the string (so there should be no other characters before BR)
BR matches - well - BR
\d is a digit (0-9)
{4} says there must be exactly 4 of the previously mentioned group (\d)
You did not specify what is allowed to follow the four digits. If this should be the end of the string, add a $.
Usage in C#:
string matching = "BR1234";
string notMatching = "someOther";
Regex regex = new Regex(#"^BR\d{4}");
bool doesMatch = regex.IsMatch(matching); // true
doesMatch = regex.IsMatch(notMatching); // false;
BR\d{4}
Some text to make answer at least 30 characters long :)
Related
I am trying to make a regex on field which accepts in the following:
Where X is a numerical value between 0-9 so 3 numbers before the - and three after the dash.
I started with the following but I got lost in adding validation after the dash.
([0-9-])\w+([0-9-])
3 digits, a dash then 3 digits:
\d{3}-\d{3}
var example = "123-455";
var pattern = #"\A(\d){3}-(\d){3}\Z";
var result = Regex.Match(example, pattern);
This will not only search for the pattern within your string, but also make sure that the beginning and end of the pattern is at the beginning and end of your target string. This ensures that you won't get a match e.g. for:
"silly123-456stuff" or "0123-4567".
In other words, it both looks for a pattern, and limits its length by anchoring it to the begining and end of the string.
string pattern = #"^([0-9]{3})-([0-9]{3})$";
Regex rgx = new Regex(pattern);
I would add the the beginning and end of line to the regex
^\d{3}-\d{3}$
^ = at the beginning of the line
\d = a number
{3} = three times
- = a dash
\d = a number
{3} = three times
$ = the end of the line
Not setting the start and end of line could catch invalid patterns, such as Text123-4858
Edit: even better than line markers, the anchors proposed by Kjartan are the correct answer in this case.
EDIT 2:
Yes like I thought, i need to change the pattern to 2 different ones, because the OR will make a match for 13 digits a match for 8 digits one
THE SOLUTION IS:
Regex EAN8 = new Regex(#"\b\d{8}\b");
Regex EAN13 = new Regex(#"\d{13}\b");
PS FOR EDIT2: As someone said, problaly in the future i will end up finding EAN1234567890123 or EAN_1234567890123, these patterns wont work out, and I have no idea where to start searching for a pattern like this.
I'm doing a project where I need to take multiple EANs from a text.
I already have a validation class to see if they are valid or not.
And I can take the 13 digits one (but I think the pattern I'm using is not correct and will give problems sooner or later.
Example of a string: "OL‐120 112 82 Estuchado, fácil apertura. 8410032002279 227 24"
as you can see there is a valid EAN13 in the middle: "8410032002279"
I'm using this pattern:
Regex EAN13 = new Regex(#"\d{13}");
It gives me the EAN inside the string, but I think the correct pattern should be like this:
Regex EAN13 = new Regex(#"\d{13}$");
But when I use it it doesn't work. probably because the string doesn't end there.
I have a similar problem with the EAN of 8 digits, if i use this pattern:
Regex EAN8 = new Regex(#"\d{8}");
It gives me the 13 digit eans cut to 8...
What should I do to make both patterns work whatever the position of the EAN is in the string and for the 8 digit return only a real string with 8 digits and not one with more cut down to 8.
Thanks in advance
EDIT: Further Code to understand what I'm doing
Regex EAN = new Regex(#"\b(?:\d{8}|\d{13})\b");
using (StreamReader sr = new StreamReader(#"....txt"))
{
string currentLine;
while ((currentLine = sr.ReadLine()) != null)
{
Match m13 = EAN.Match(currentLine);
Match m8 = EAN.Match(currentLine);
if (m8.Success)
{
lista_EAN8.Add(m8.Value);
//string valido8 = new Ean8Validate().ValidateEan8(m8.Value);
//if (valido8 == m8.Value)
//{
// lista_EAN8.Add(m8.Value);
//}
}
if (m13.Success)
{
string valido13 = new Ean13Validate().ValidateEan13(m13.Value);
if (valido13 == m13.Value)
{
lista_EAN13.Add(m13.Value);
}
}
}
}
Like this it returns me the same values in list of 13 digit eans and list of 8 digits eans
ok looks like you want 2 different Regexs one for targeting only 8 digit matches and the other for targeting 13 digit matches
for matching of the 8 digit EANs use;
\b(?:\d{8})\b
for matching and for 13 digit EANs use;
\b(?:\d{13})\b
additionally is you want an options prefix of EAN(upper or lowercase) you can use;
for 8 digit
\b(?:[Ee][Aa][Nn])?(?:\d{8})\b
for 13 digit
\b(?:[Ee][Aa][Nn])?(?:\d{8})\b
for your example you want to modify the code so it read something like this.
Regex EAN8 = new Regex(#"\b(?:\d{8})\b");
Regex EAN13 = new Regex(#"\b(?:\d{13})\b");
using (StreamReader sr = new StreamReader(#"....txt"))
{
string currentLine;
while ((currentLine = sr.ReadLine()) != null)
{
Match m13 = EAN13.Match(currentLine);
Match m8 = EAN8.Match(currentLine);
if (m8.Success)
{
lista_EAN8.Add(m8.Value);
}
if (m13.Success)
{
lista_EAN13.Add(m13.Value);
}
}
}
now if we tweek the Regexs a little more we can extract just the number parts out of EAN numbers even when they are prefixed with EAN* or EAN_*
Regex EAN8 = new Regex(#"\b(?:[Ee][Aa][Nn]_?)?(\d{8})\b");
Regex EAN13 = new Regex(#"\b(?:[Ee][Aa][Nn]_?)?(\d{13})\b");
using (StreamReader sr = new StreamReader(#"....txt"))
{
string currentLine;
while ((currentLine = sr.ReadLine()) != null)
{
Match m13 = EAN13.Match(currentLine);
Match m8 = EAN8.Match(currentLine);
if (m8.Success)
{
lista_EAN8.Add(m8.Groups[1].Value);
}
if (m13.Success)
{
lista_EAN13.Add(m13.Groups[1].Value);
}
}
}
this will capture the number part while throwing away the EAN prefix
Use the below regex to match 8 or 13 digits. \b is a word boundary which matches between a word character and a non-word character. So it avoids matching 8 digit number in a 13 digit number.
\b(?:\d{8}|\d{13})\b
Try this regex string. \b = word boundary and the | makes sure that it will only match 8 or 13 and not some number in between:
\b\d{8}\b|\b\d{13}\b
If you want dob't allow unicode digit use character class instead shortcut \d ( it's much faster)
\b(?:[0-9]{8}|[0-9]{13})\b
I managed to concoct this:
\b(([Ee][Aa][Nn])?[_]?([0-9]{13}|[0-9]{8}))\b
This part ([Ee][Aa][Nn])? groups the case-insensitive sequence EAN and makes it optional with ?
Then [_]? makes the underscore optional. I added the square brackets for prettiness sake
The digits are identified using their character representation [0-9]{13} and [0-9]{8}
Everything is wrapped up in a \b( ... )\b block to identify the word boundary
The two EAN types are wrapped by parentheses and separated by an OR |
Below is a screenshot from http://regexpal.com/ showing the testing set and the matching.
Jorge, I must say I don't like repeating code, (or anything else for that matter :D ). I am therefore not very fond of the whole ([Ee][Aa][Nn])?[_]? appearing twice. Moreover if tomorrow you wish to look for EAN5 for example you must further copy it and the regexp becomes ever more ugly.
Here is what I had before the cleanup:
\b(([Ee][Aa][Nn])?[_]?[0-9]{13}|([Ee][Aa][Nn])?[_]?[0-9]{8})\b
Below is a screenshot from http://regexpal.com/ showing the testing set and the matching.
I would like to use regex instead of string.replace() to get the first 6 chars of a string and the last 4 chars of the same string and substitute it with another character: & for example. The string is always with 16 chars. Im doing some research but i never worked with regex before. Thanks
If you prefer to use regular expression, you could use the following. The dot . will match any character except a newline sequence, so you can specify {n} to match exactly n times and use beginning/end of string anchors.
String r = Regex.Replace("123456foobar7890", #"^.{6}|.{4}$",
m => new string('&', m.ToString().Length));
Console.WriteLine(r); //=> "&&&&&&foobar&&&&"
If you want to invert the logic, replacing the middle portion of your string you can use Positive Lookbehind.
String r = Regex.Replace("123456foobar7890", #"(?<=^.{6}).{6}",
m => new string('&', m.ToString().Length));
Console.WriteLine(r); //=> "123456&&&&&&7890"
I need a regex for check this format:
[some digits][some digits][some digits][some digits][some digits][some digits]#
"some digits" means each number (0 or 1 or 2 or 3 or .... ), 2 digits, 3 digits, or more...
but it's important that each open bracket be closed before another open one...
actually I want to check the format and also get the number of [].
I tried this code for getting number of [] :
Regex.Matches( input, "[]" ).Count
but it didnt work.
thanks for helping
This is the regex you're looking for:
^(\[\d+\])+#$
See the demo.
Sample Code for the Count
var myRegex = new Regex(#"^(\[\d+\])+#$");
string bracketCount = myRegex.Match(yourString).Groups[1].Count;
Explanation
The ^ anchor asserts that we are at the beginning of the string
( starts capture Group 1
\[opens a bracket
\d+ matches one or more digits
\] matches the closing bracket
) closes Group 1
+ matches this 1 or more times
# the hash
The $ anchor asserts that we are at the end of the string
I currently have a regex that pulls up a 16 digit number from a file e.g.:
Regex:
Regex.Match(l, #"\d{16}")
This would work well for a number as follows:
1234567891234567
Although how could I also include numbers in the regex such as:
1234 5678 9123 4567
and
1234-5678-9123-4567
If all groups are always 4 digit long:
\b\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}\b
to be sure the delimiter is the same between groups:
\b\d{4}(| |-)\d{4}\1\d{4}\1\d{4}\b
If it's always all together or groups of fours, then one way to do this with a single regex is something like:
Regex.Match(l, #"\d{16}|\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4}")
You could try something like:
^([0-9]{4}[\s-]?){3}([0-9]{4})$
That should do the trick.
Please note:
This also allows
1234-5678 9123 4567
It's not strict on only dashes or only spaces.
Another option is to just use the regex you currently have, and strip all offending characters out of the string before you run the regex:
var input = fileValue.Replace("-",string.Empty).Replace(" ",string.Empty);
Regex.Match(input, #"\d{16}");
Here is a pattern which will get all the numbers and strip out the dashes or spaces. Note it also checks to validate that there is only 16 numbers. The ignore option is so the pattern is commented, it doesn't affect the match processing.
string value = "1234-5678-9123-4567";
string pattern = #"
^ # Beginning of line
( # Place into capture groups for 1 match
(?<Number>\d{4}) # Place into named group capture
(?:[\s-]?) # Allow for a space or dash optional
){4} # Get 4 groups
(?!\d) # 17th number, do not match! abort
$ # End constraint to keep int in 16 digits
";
var result = Regex.Match(value, pattern, RegexOptions.IgnorePatternWhitespace)
.Groups["Number"].Captures
.OfType<Capture>()
.Aggregate (string.Empty, (seed, current) => seed + current);
Console.WriteLine ( result ); // 1234567891234567
// Shows False due to 17 numbers!
Console.WriteLine ( Regex.IsMatch("1234-5678-9123-45678", pattern, RegexOptions.IgnorePatternWhitespace));