Regex for checking numbers in a string - c#

I am looking for help with a regex for checking a string that could contain 10 digits separated by other characters or alphabets. For example
call1234567890
1234567890call
12.34_567.890_call
I have tried \D*(\d\D*){10}$ as suggested in other posts , but this matches with any string that has numbers even if 1 and characters after 1. So
Silly_1_me is also being caught

You must need to include starting anchor ^ so that it would do an exact line match or otherwise, it would do a partial string match.
#"^\D*(\d\D*){10}$"
DEMO
For multiline input , its better to use the below regex.
#"^[^\n\d]*(\d[^\n\d]*){10}$"

^(?!(?:.*\d){11,})(?:.*\d){10}[^\d]*$
Try this.See demo.
http://regex101.com/r/hQ9xT1/21

Related

Having trouble matching the 17 characters after the first comma

I am trying to match 17 characters after the first comma. The string ends at the second comma. How do I match between these two commas using regex? Is there a way to match the string if it varies in length? Below is my test string.
0 PSC_OK,MACESBCE218002001,C07KTL89290003;1,C07KTL89290003,1;2,C07KTL89290003,0;3,C07KTL89290003,0;4,C07KTL89290003,0;5,C07KTL89290003,0;6,C07KTL89290003,0;7,C07KTL89290003,0;8,C07KTL89290003,0;9,C07KTL89290003,0;10,C07KTL89290003,0;11,C07KTL89290003,0;12,C07KTL89290003,0;13,C07KTL89290003,0;14,C07KTL89290003,0;15,C07KTL89290003,0;16,C07KTL89290003,0;17,C07KTL89290003,0;18,C07KTL89290003,0;19,C07KTL89290003,0;20,C07KTL89290003,0;21,C07KTL89290003,0;22,C07KTL89290003,0;23,C07KTL89290003,0;24,C07KTL89290003,0;25,C07KTL89290003,0;26,C07KTL89290003,0;27,C07KTL89290003,0;28,C07KTL89290003,0;29,C07KTL89290003,0;30,C07KTL89290003,0;31,C07KTL89290003,0;32,C07KTL89290003,0;33,C07KTL89290003,0;34,C07KTL89290003,0;35,C07KTL89290003,0;36,C07KTL89290003,0;37,C07KTL89290003,0;38,C07KTL89290003,0;39,C07KTL89290003,0;40,C07KTL89290003,0
Yes, you can build a regex expression like
/(,?)([a-zA-Z0-9;_]+)(,?)/gi
With Regex Expression you can get any match or group, the sitaxis change according to language, but, it almost the same.
And you can test it in page Regexr

Extract string from a pattern preceded by any length

I'm looking for a regular expression to extract a string from a file name
eg if filename format is "anythingatallanylength_123_TESTNAME.docx", I'm interested in extracting "TESTNAME" ... probably fixed length of 8. (btw, 123 can be any three digit number)
I think I can use regex match ...
".*_[0-9][0-9][0-9]_[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z].docx$"
However this matches the whole thing. How can I just get "TESTNAME"?
Thanks
Use parenthesis to match a specific piece of the whole regex.
You can also use the curly braces to specify counts of matching characters, and \d for [0-9].
In C#:
var myRegex = new Regex(#"*._\d{3}_([A-Za-z]{8})\.docx$");
Now "TESTNAME" or whatever your 8 letter piece is will be found in the captures collection of your regex after using it.
Also note, there will be a performance overhead for look-ahead and look-behind, as presented in some other solutions.
You can use a look-behind and a look-ahead to check parts without matching them:
(?<=_[0-9]{3}_)[A-Z]{8}(?=\.docx$)
Note that this is case-sensitive, you may want to use other character classes and/or quantifiers to fit your exact pattern.
In your file name format "anythingatallanylength_123_TESTNAME.docx", the pattern you are trying to match is a string before .docx and the underscore _. Keeping the thing in mind that any _ before doesn't get matched I came up with following solution.
Regex: (?<=_)[A-Za-z]*(?=\.docx$)
Flags used:
g global search
m multi-line search.
Explanation:
(?<=_) checks if there is an underscore before the file name.
(?=\.docx$) checks for extension at the end.
[A-Za-z]* checks the required match.
Regex101 Demo
Thanks to #Lucero #noob #JamesFaix I came up with ...
#"(?<=.*[0-9]{3})[A-Z]{8}(?=.docx$)"
So a look behind (in brackets, starting with ?<=) for anything (ie zero or more any char (denoted by "." ) followed by an underscore, followed by thee numerics, followed by underscore. Thats the end of the look behind. Now to match what I need (eight letters). Finally, the look ahead (in brackets, starting with ?=), which is the .docx
Nice work, fellas. Thunderbirds are go.

I want only matching string using regex

I have a string "myname 18-may 1234" and I want only "myname" from whole string using a regex.
I tried using the \b(^[a-zA-Z]*)\b regex and that gave me "myname" as a result.
But when the string changes to "1234 myname 18-may" the regex does not return "myname". Please suggest the correct way to select only "myname" whole word.
Is it also possible - given the string in
"1234 myname 18-may" format - to get myname only, not may?
UPDATE
Judging by your feedback to your other question you might need
(?<!\p{L})\p{L}+(?!\p{L})
ORIGINAL ANSWER
I have come up with a lighter regex that relies on the specific nature of your data (just a couple of words in the string, only one is whole word):
\b(?<!-)\p{L}+\b
See demo
Or even a more restrictive regex that finds a match only between (white)spaces and string start/end:
(?<=^|\s)\p{L}+(?=\s|$)
The following regex is context-dependent:
\p{L}+(?=\s+\d{1,2}-\p{L}{3}\b)
See demo
This will match only the word myname.
The regex means:
\p{L}+ - Match 1 or more Unicode letters...
(?=\s+\d{1,2}-\p{L}{3}\b) - until it finds 1 or more whitespaces (\s+) followed with 1 or 2 digits, followed with a hyphen and 3 Unicode letters (\p{L}{3}) which is a whole word (\b). This construction is a positive look-ahead that only checks if something can be found after the current position in the string, but it does not "consume" text.
Since the date may come before the string, you can add an alternation:
\p{L}+(?=[ ]+\d{1,2}-\p{L}{3}\b)|(?<=\d{1,2}-\p{L}{3}[ ]+)\p{L}+
See another demo
The (?<=\d{1,2}-\p{L}{3}\s+) is a look-behind that checks for the same thing (almost) as the look-ahead, but before the myname.
here is a solution without RegEx
string input = "myname 18-may 1234";
string result = input.Split(' ').Where(x => x.All(y => char.IsLetter(y))).FirstOrDefault();
Do a replace using this regex:
(\s*\d+\-.{3}\s*|\s*.{3}\-\d+\s*)|(\s*\d+\s*)
you will end up with just your name.
Demo

Regex issue i am stuck with

I have to write a regex for matching a pattern 1-6/2011.
In this case, digits before the / can not be greater than 12.
So I have to select digits between 1-12.
I have written a regex:
^[1-9][0-2]?\s*[-−—]\s*[1-9][0-2]?\s*/\s*2[01][0-9][0-9]$
However, here I am getting 20-6/2014 also as a match.
I tried with a negative look-behind:
^[1-9](?<![2-9])[0-2]?\s*[-−—]\s*[1-9](?<![2-9])[0-2]?\s*/\s*2[01][0-9][0-9]$
Here, single digits are not getting identified.
You can use the following update of your regex:
^(?:0?[1-9]|1[0-2])\s*[-−—]\s*(?:0?[1-9]|1[0-2])\s*/\s*\s*2[01][0-9]{2}$
See demo
It will not match 12-30/2014, 12-31/2014, 12-32/2014, 13-31/2014, 20-6/2014.
It will match 1-6/2011 and 02-12/2014.
C#:
var lines = "1-6/2011\r\n02-12/2014\r\n12-30/2014\r\n12-31/2014\r\n12-32/2014\r\n13-31/2014\r\n20-6/2014";
var finds = Regex.Matches(lines, #"^(?:0?[1-9]|1[0-2])\s*[-−—]\s*(?:0?[1-9]|1[0-2])\s*/\s*\s*2[01][0-9]{2}\r?$", RegexOptions.Multiline);
Mind that \r? is only necessary in case we test with Multiline mode on. You can remove it when checking separate values.
So i have to select digits between 1-12
For that you can use regex
(?:0?[1-9]|1[0-2])
See demo.
https://www.regex101.com/r/fJ6cR4/23
You can use this regex:
^(?:[1-9]|1[0-2])\s*-\s*(?:[1-9]|1[0-2])\s*/\s*2[01]\d{2}$
RegEx Demo
Simplest regex to match with 1-12 is (1[0-2]?)|[2-9].
It matches with 13 cause 1[0-2]? matches with 1, but it doesn't matter in full regex (1[0-2]?)|[2-9]\/\d\d\d\d.

Check regex for letters numbers and underscore characters

I tried to check if a string Name contains letters, numbers, and underscore character with
the following code without success, any idea of what I miss here?
var regex = new Regex(#"^[a-zA-Z0-9]+$^\w+$");
if (regex.IsMatch(Name) )
....
in addtion when I tried with the following code, I got a parsing error "^[a-zA-Z0-9\_]+$" - Unrecognized escape sequence \_.
Var regex = new Regex(#"^[a-zA-Z0-9\_]+$");
The regex should be:
#"^[a-zA-Z0-9_]+$"
You don't need to escape the underscore. You can also use the Regex.Ignorecase option, which would allow you to use #"^[a-z0-9_]+$" just as well.
Try this regex
^[a-zA-Z0-9_-]$
You can match name with length also by this regex
^[a-zA-Z0-9_-]{m,n}$
Where
m is the start index
n is the end index
Regex Demo
Take a look at here

Categories