test a specific string with regex - c#

i'm going to test a string of the form dd / mm / yyyy xx-xxxx-x xxx-xxx with a string array, i use to define the regex form but i think the format is not correctly declare
Regex rgx1 = new Regex(#"^d{2}\/\d{2}\/\d{4}\t[A-Z]\d{2}\-\d{4}\-\[A-Z0-9]\d{1}\t[A-Z]\d{3}\-\[A-Z]\d{3}$");
Match FormatS = rgx1.Match(tab[i]);
if ( FormatS.Success)
{
Console.WriteLine(tab[i]);
Console.ReadLine();
}

Based on your comment with sample input, this works:
Regex rgx1 = new Regex(#"^\d{2}/\d{2}/\d{4}\s[A-Z]{2}-\d{4}-[A-Z0-9]{1}\s[A-Z]{3}-[A-Z]{3}$");
Problems I found:
\[ instead of [ in two places
d instead of \d at the start
\t instead of \s (or just a space would probably be fine, too)
a few unnecessary \d
I also removed a few unnecessarily-escaped tokens, but... those don't matter as much.

Related

Regex pattern generator

I'm trying to do regex pattern which will match to this:
Name[0]/Something
or
Name/Something
Verbs Name and Something will be always known.
I did for Name[0]/Something, but I want make pattern for this verb in one regex
I've tried to sign [0] as optional but it didn't work :
var regexPattern = "Name" + #"\([\d*\]?)/" + "Something"
Do you know some generator where I will input some verbs and it will make pattern for me?
Use this:
Name(\[\d+\])?\/Something
\d+ allows one or more digits
\[\d+\] allows one or more digits inside [ and ]. So it will allow [0], [12] etc but reject []
(\[\d+\])? allows digit with brackets to be present either zero times or once
\/ indicates a slash (only one)
Name and Something are string literals
Regex 101 Demo
You were close, the regex Name(\[\d+\])?\/Something will do.
The problem is with first '\' in your pattern before '('.
Here is what you need:
var str = "Name[0]/Something or Name/Something";
Regex rg = new Regex(#"Name(\[\d+\])?/Something");
var matches = rg.Matches(str);
foreach(Match a in matches)
{
Console.WriteLine(a.Value);
}
var string = 'Name[0]/Something';
var regex = /^(Name)(\[\d*\])?\/Something$/;
console.log(regex.test(string));
string = 'Name/Something';
console.log(regex.test(string));
You've tried wrong with this pattern: \([\d*\]?)/
No need to use \ before ( (in this case)
? after ] mean: character ] zero or one time
So, if you want the pattern [...] displays zero or one time, you can try: (\[\d*\])?
Hope this helps!
i think this is what you are looking for:
Name(\[\d+\])?\/Something
Name litteral
([\d+])? a number (1 or more digits) between brackets optional 1 or 0 times
/Something Something litteral
https://regex101.com/r/G8tIHC/1

String.Format - query metadata from the format string

Is there any function to query the expected inputs and formats from a format string - i.e. one intended as the first argument to the String.Format function?
e.g. given:
"On {0:yyyyy-MM-dd} do {1} and earn {2:C2}"
I'd like to get back something like:
{"yyyyy-MM-dd", null, "C2"}
I guess a regex is one possibility but is there anything precanned that hooks into the same logic as String.Format?
String.Format itself doesn't parse the format string. It ends up calling the internal StringBuilder.AppendFormatHelper method which treats the format strings only as delimited strings. It doesn't try to parse them. The format is passed directly to each argument type's formatter method. String formatting performance is critical, both for the runtime and applications.
You can use a regular expression to parse the format string. You'd need to take care of escaped braces ({{, {}) and alignment strings.
The regex {(?<index>\d+)(,(?<algn>-?\d+?))?(:(?<fmt>.*?))?} takes extracts the index, alignment and format segments as named groups. It doesn't take care of escaped braces *explicitly. It will avoid {{ {} but not {{2,20:N{}:
var regex=new System.Text.RegularExpressions.Regex(#"{(?<index>\d+)(,(?<algn>-?\d+?))?(:(?<fmt>.*?))?}");
var matches=regex.Matches("asdf{0:d2} {1:yyyy-MM-dd} {2,-20:N2}");
foreach(Match match in matches)
{
Console.WriteLine("{0,-5} {1,-15} {2,-15}",
match.Groups["index"].Value,
match.Groups["algn"].Value,
match.Groups["fmt"].Value);
}
This will return :
0 d2
1 yyyy-MM-dd
2 -20 N2
The (?<name>...) syntax captures a pattern and exposes it as a named group. (?<index>\d+) captures a sequence of digits and exposes it as the group index.
The ? in .*? specifies a non-greedy match. Typically a regex is greedy - it will capture as many characters match a pattern as possible. By using .*? the regex will capture as few characters as possible before the next pattern starts. That's why the optional algn group stops at :.
Chances are no standard means for that. Use Regex, it's easy:
var args = new List<string>();
var str = "On {0:yyyyy-MM-dd} do {1} and earn {2:C2}";
MatchCollection matches = Regex.Matches(str, #"\{\d+[^\{\}]*\}");
foreach (Match match in matches)
{
string obj = null;
var split = match.ToString().Split(':');
if (split.Length == 2) obj = split.Last().Trim(' ', '}', '{');
args.Add(obj);
}
// Result: args = {"yyyyy-MM-dd", null, "C2"}

how to extract date from a string using regex

i m looking for regex which can extract the date from the following html
<p>British Medical Journal, 29.9.12, pp.37-41.</p>
and convert it in the format 29/09/12
Match this pattern: -
(\d+)[.](\d+)[.](\d+)
and replace with: -
$1/$2/$3
\d is used to match digits. Using it with quantifier (+), you would match one or more digits.
Now, in regex, a dot(.) is a metacharacter, that matches any character. To match a period literally, you would need to either escape it, or use it inside a character class.
To convert to a specific Date Format, e.g.: - convert 9 -> 09, you can make use of a MatchEvaluator: -
string input = "British Medical Journal, 29.9.12, pp.37-41.";
Regex reg = new Regex(#"(\d+)[.](\d+)[.](\d+)");
string result = reg.Replace(input, delegate(Match m) {
return m => DateTime.Now.ToString("dd/MM/yy")
});
You can check whether it works or not.
Here is the regex pattern: \d{1,2}\.\d{1,2}\.\d{1,2}.
And here is the example of how to parse this string to DateTime:
DateTime.ParseExact("29.9.12", "d.M.yy", CultureInfo.InvariantCulture);
(\d{4})[-](\d{2})[-](\d{2}) use this regex to pick 2017-01-23 format date

C# Regex for retrieving capital string in quotation mark

Given a string, I want to retrieve a string that is in between the quotation marks, and that is fully capitalized.
For example, if a string of
oqr"awr"q q"ASRQ" asd "qIKQWIR"
has been entered, the regex would only evaluate "ASRQ" as matching string.
What is the best way to approach this?
Edit: Forgot to mention the string takes a numeric input as well I.E: "IO8917AS" is a valid input
EDIT: If you actually want "one or more characters, and none of the characters is a lower-case letter" then you probably want:
Regex regex = new Regex("\"\\P{Ll}+\"");
That will then allow digits as well... and punctuation. If you want to allow digits and upper case letters but nothing else, you can use:
Regex regex = new Regex("\"[\\p{Lu}\\d]+\"");
Or in verbatim string literal form (makes the quotes more confusing, but the backslashes less so):
Regex regex = new Regex(#"""[\p{Lu}\d]+""");
Original answer (before digits were required)
Sounds like you just want (within the pattern)
"[A-Z]*"
So something like:
Regex regex = new Regex("\"[A-Z]*\"");
Or for full Unicode support, use the Lu Unicode character category:
Regex regex = new Regex("\"\\p{Lu}*\"");
EDIT: As noted, if you don't want to match an empty string in quotes (which is still "a string where everything is upper case") then use + instead of *, e.g.
Regex regex = new Regex("\"\\p{Lu}+\");
Short but complete example of finding and displaying the first match:
using System;
using System.Text.RegularExpressions;
class Program
{
public static void Main()
{
Regex regex = new Regex("\"\\p{Lu}+\"");
string text = "oqr\"awr\"q q\"ASRQ\" asd \"qIKQWIR\"";
Match match = regex.Match(text);
Console.WriteLine(match.Success); // True
Console.WriteLine(match.Value); // "ASRQ"
}
}
Like this:
"\"[A-Z]+\""
The outermost quotes are not part of the regex, they delimit a C# string.
This requires at least one uppercase character between quotes and works for the English language.
Please try the following:
[\w]*"([A-Z0-9]+)"

Regex for numbers only

I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I want the regex to match only when the contained string is all numbers; but with the two examples below it is matching a string that contains all numbers plus an equals sign like "1234=4321". I'm sure there's a way to change this behavior, but as I said, I've never really done much with regular expressions.
string compare = "1234=4321";
Regex regex = new Regex(#"[\d]");
if (regex.IsMatch(compare))
{
//true
}
regex = new Regex("[0-9]");
if (regex.IsMatch(compare))
{
//true
}
In case it matters, I'm using C# and .NET2.0.
Use the beginning and end anchors.
Regex regex = new Regex(#"^\d$");
Use "^\d+$" if you need to match more than one digit.
Note that "\d" will match [0-9] and other digit characters like the Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩. Use "^[0-9]+$" to restrict matches to just the Arabic numerals 0 - 9.
If you need to include any numeric representations other than just digits (like decimal values for starters), then see #tchrist's comprehensive guide to parsing numbers with regular expressions.
Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:
regex = new Regex("^[0-9]+$");
The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).
If you need to tolerate decimal point and thousand marker
var regex = new Regex(#"^-?[0-9][0-9,\.]+$");
You will need a "-", if the number can go negative.
This works with integers and decimal numbers. It doesn't match if the number has the coma thousand separator ,
"^-?\\d*(\\.\\d+)?$"
some strings that matches with this:
894
923.21
76876876
.32
-894
-923.21
-76876876
-.32
some strings that doesn't:
hello
9bye
hello9bye
888,323
5,434.3
-8,336.09
87078.
It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.
^\d+$
Perhaps my method will help you.
public static bool IsNumber(string s)
{
return s.All(char.IsDigit);
}
If you need to check if all the digits are number (0-9) or not,
^[0-9]+$
Matches
1425
0142
0
1
And does not match
154a25
1234=3254
Sorry for ugly formatting.
For any number of digits:
[0-9]*
For one or more digit:
[0-9]+
^\d+$, which is "start of string", "1 or more digits", "end of string" in English.
Here is my working one:
^(-?[1-9]+\\d*([.]\\d+)?)$|^(-?0[.]\\d*[1-9]+)$|^0$
And some tests
Positive tests:
string []goodNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};
Negative tests:
string []badNums={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};
Checked not only for C#, but also with Java, Javascript and PHP
Use beginning and end anchors.
Regex regex = new Regex(#"^\d$");
Use "^\d+$" if you need to match more than one digit.
While non of the above solutions was fitting my purpose, this worked for me.
var pattern = #"^(-?[1-9]+\d*([.]\d+)?)$|^(-?0[.]\d*[1-9]+)$|^0$|^0.0$";
return Regex.Match(value, pattern, RegexOptions.IgnoreCase).Success;
Example of valid values:
"3",
"-3",
"0",
"0.0",
"1.0",
"0.7",
"690.7",
"0.0001",
"-555",
"945465464654"
Example of not valid values:
"a",
"",
" ",
".",
"-",
"001",
"00.2",
"000.5",
".3",
"3.",
" -1",
"--1",
"-.1",
"-0",
"00099",
"099"
Another way: If you like to match international numbers such as Persian or Arabic, so you can use following expression:
Regex = new Regex(#"^[\p{N}]+$");
To match literal period character use:
Regex = new Regex(#"^[\p{N}\.]+$");
Regex for integer and floating point numbers:
^[+-]?\d*\.\d+$|^[+-]?\d+(\.\d*)?$
A number can start with a period (without leading digits(s)),
and a number can end with a period (without trailing digits(s)).
Above regex will recognize both as correct numbers.
A . (period) itself without any digits is not a correct number.
That's why we need two regex parts there (separated with a "|").
Hope this helps.
I think that this one is the simplest one and it accepts European and USA way of writing numbers e.g. USA 10,555.12 European 10.555,12
Also this one does not allow several commas or dots one after each other e.g. 10..22 or 10,.22
In addition to this numbers like .55 or ,55 would pass. This may be handy.
^([,|.]?[0-9])+$
console.log(/^(0|[1-9][0-9]*)$/.test(3000)) // true
If you want to extract only numbers from a string the pattern "\d+" should help.
To check string is uint, ulong or contains only digits one .(dot) and digits
Sample inputs
Regex rx = new Regex(#"^([1-9]\d*(\.)\d*|0?(\.)\d*[1-9]\d*|[1-9]\d*)$");
string text = "12.0";
var result = rx.IsMatch(text);
Console.WriteLine(result);
Samples
123 => True
123.1 => True
0.123 => True
.123 => True
0.2 => True
3452.434.43=> False
2342f43.34 => False
svasad.324 => False
3215.afa => False
The following regex accepts only numbers (also floating point) in both English and Arabic (Persian) languages (just like Windows calculator):
^((([0\u0660\u06F0]|([1-9\u0661-\u0669\u06F1-\u06F9][0\u0660\u06F0]*?)+)(\.)[0-9\u0660-\u0669\u06F0-\u06F9]+)|(([0\u0660\u06F0]?|([1-9\u0661-\u0669\u06F1-\u06F9][0\u0660\u06F0]*?)+))|\b)$
The above regex accepts the following patterns:
11
1.2
0.3
۱۲
۱.۳
۰.۲
۲.۷
The above regex doesn't accept the following patterns:
3.
.3
0..3
.۱۲
Regex regex = new Regex ("^[0-9]{1,4}=[0-9]{1,4]$")

Categories