I want to validate that a string follows this format (using regex):
valid: 123456789 //9 digits
valid: 12-1234567 // 2 digits + dash + 7 digits
Here's an example, how I would use it:
var r = new Regex("^[1-9]\d?-\d{7}$");
Console.WriteLine(r.IsMatch("1-2-3"));
I have the regex for the format with dash, but can't figure how to include the non-dash format???
Regex regex = new Regex("^\\d{2}-?\\d{7}$");
This will accept the two formats you want: 2 digits then an optional dash and 7 numbers.
^ \d{9} | \d{2} - \d{7} $
Remove the spaces, they are there for readability.
Related
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 :)
I'm looking for a Regex to validate a rating (1-10) + optional text.
Rating is a decimal, with 1 point, which can use both dot or comma separator.
Followed by an optional space + string.
Valid
7
7,5
7.5
7,5 This is my string
7.5 Hello
Invalid
7,75
11
7This is my string
7.This is my string
10.5 string
I've got this for getting the decimal values, but I'm not sure how to get the optional text behind it.
^(10|\d)([\.\,]\d{1,1})?$
Judging by your examples, the space after the initial number is not optional. Thus, the pattern you may use is
^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$
or - since a partial match with Regex.IsMatch is enough to validate the string - replace (?:\s.*)?$ with a negative lookahead (?!\S) that will require a whitespace or end of string after the number:
^(?:10|[1-9](?:[.,][0-9])?)(?!\S)
^^^^^^^
See the regex demo
Details:
^ - start of a string
(?:10|[1-9](?:[.,][0-9])?) - either 10 or a digit from 1 to 9 followed with an optional sequence of a , or . and any single digit and then...
(?:\s.*)?$ - an optional sequence of any whitespace followed with any chars up to the end of string - OR -
(?!\S) - a negative lookahead that fails the match if there is no non-whitespace char immediately to the right of the current position.
C# test:
var strs = new List<string> { "7","7,5","7.5","7,5 This is my string","7.5 Hello","7,75","11","7This is my string","7.This is my string","10.5 string"};
var pattern = #"^(?:10|[1-9](?:[.,][0-9])?)(?:\s.*)?$";
foreach (var s in strs)
if (Regex.IsMatch(s, pattern))
Console.WriteLine("{0} is correct.", s);
else
Console.WriteLine("{0} is invalid.", s);
Output:
7 is correct.
7,5 is correct.
7.5 is correct.
7,5 This is my string is correct.
7.5 Hello is correct.
7,75 is invalid.
11 is invalid.
7This is my string is invalid.
7.This is my string is invalid.
10.5 string is invalid.
I have this particular string:
Administrationsomkostninger I -2.889 - r0.l l0
I would like to replace these characters:r,l and i with 1.
I use this expression:
([(t|r|l|i|)])
That gives me this string:
Adm1n1s11a11onsomkos1n1nge1 1 -2.889 - 10.1 10
Now i want to replace the all digits that contains a digit followed + a whitespace
so in this case only - 10.1 10 gets converted to -10.110
Try this
string input = "Administrationsomkostninger I -2.889 - r0.l l0";
string pattern = #"(?'spaces'\s){2,}";
string output = Regex.Replace(input, pattern, " ");
I have the following sample string:
string s = Console.ReadLine();
s= {6} {7613023456148 } {7.040 } {56780} {Sample String}
How do I achieve the following with regex or something similar:
Remove all numbers in a line that start with 7 and are 13 digits long.
Remove all decimal numbers.
Output
s = {6} {56780} {Sample String}
You can replace the captured string use following regex with an empty string :
7\d{12}|\d\.\d+
But Note that if your numbers are within {} you need :
\b{\s*7\d{12}\s*}\b|\b{\s*\d+\.\d+\s*}\b
See demo https://regex101.com/r/dL1vF4/1
You are looking for this regex:
var s = "{6} {7613023456148 } {7.040 } {56780} {Sample String}";
s = Regex.Replace(s, #"\s*{(?:\s*7[0-9]{12}\s*|\d+\.\d+\s*)}", string.Empty);
Console.WriteLine(s); // ==> "{6} {56780} {Sample String}"
See IDEONE demo
REGEX matches 2 alternatives inside {...} that is preceded with optional whitespace (\s*):
\s*7[0-9]{12}\s* - optional whitespace followed with7`, then 12 digits and optional whitespace
\d+\.\d+\s* - 1 or more digits, a . decimal separator, again 1 or more digits, and optional whitespace.
Since all your values are inside {...}, you need no word boundaries.
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));