Regex to check time period value 1W, 5Y - c#

Here I need to validate a string like this in C#:
The string's max length: 4
The last character should be any one from D, W, M, Y (representing day,week,month,year)
The rest(initial) all characters should be a non negative number.
Example:
Valid values : "1D", "4w", "26W", "3M","24M","5Y" etc
Invalid values : "M3","360DM","5555D", "1Y2M", "-5Y" etc

You could use the following regular expression (with the case-indifferent flag set).
^(?:0|[1-9]\d{0,2})[DWMY]$
Demo
The regex engine performs the following operations.
^ match the beginning of the string
(?: begin a non-capture group
0 match '0'
| or
[1-9] match a digit other than '0'
\d{0,2} match 0-2 digits
) end non-capture group
[DWMY] match one of the characters indicated
$ match end of string

Bad Question, nontheless that's quite basic for a regex: string pattern = "([0-9]{1-3})([YDWM])"; it's up to you to refine this pattern for case sensitivity. you have multiple choices on how to handle that

Related

Regular Expression for negative accounting money values?

I am having a hard time finding a solution for negative accounting/money values that look like:
(1.00)
(100)
(100.00)
I've tried this, but for some reason, it's allowing values such as 'abcd'.
^0\.00||(\(\d*(?:\.\d{1,2})?\))?$
It does not match abcd, but if you test if the string has a match, then it will be true as there are positions that match.
As all the digits are optional at the end of the string, there is a position that can match.
Also the || matches a position after 0.00, before and after the digits part and on every position in abcd or an empty string.
You can change the || to |, match at least a single digit and use the alternation for both alternatives:
^(?:0\.00|\(\d+(?:\.\d{1,2})?\))$
^ Start of string
(?: Non capture group for the alternation
0\.00 Match 0.00
| Or
\(\d+(?:\.\d{1,2})?\) Match ( 1+ digits, optionally . and 1-2 digits and )
) Close non capture group
$ End of string
Regex demo

Regular expression to validate a mathematical formula

I need to validate a string using regex to confirm whether it is following a valid format.
The string can contain numbers, operators, space, dot, left parenthesis, right parenthesis, comma, these aggregate functions SUM, MAX, MIN, AVG and variables starting with letter V.
I found this regex ^[0-9+-/()., ]+$ this checks 0-9 (numbers); '+'; '-'; ''; '/'; '('; ')'; '.'; ','; ' '(space). But I am not able to include aggregate functions and letter V in this.
Some of the valid input strings are
AVG(SUM(1, 2, 3), SUM(4, 5, 6)) * 100
SUM(V1/2,(2+7),3)+(V1+V2)
Can someone please help me on this.
From the comments on the question:
Are you trying to ensure that only valid characters, aggregate functions, and variable names appear in the string or are you attempting to also check that the string is well formatted (i.e. there is an operand on either side of an operator, parenthesis are matched, etc...)?
- D M
#D M I am just trying to validate only for valid characters
- DevMJ
Since you're only looking to check that a formula contains digits, functions, variables, etc (and not that it is also valid for execution), you can add possibilities as alternatives in one group.
One possibility is the pattern ^(?:\d|\+|\-|\/|\*|\(|\)|\.|\,|AVG|SUM|MAX|MIN|V\d+| )*$ which matches the samples you provided.
Try it out!
Explanation:
Token
Matches
^
Start of a line
(?:
Start of the non-capturing group of alternatives
\d
A digit (equivalent to [0-9])
\+
The + character
\-
The - character
\/
The / character
\*
The * character
\(
The ( character
\)
The ) character
\.
The . character
\,
The , character
AVG
The string AVG
SUM
The string SUM
MAX
The string MAX
MIN
The string MIN
V\d+
The V character followed by one or more digits
A space
)
End of the non-capturing group of alternatives
*
Any of the alternatives zero or more times
$
End of a line
As mentioned in the comments, if you also want to check that the string can be executed successfully, you will need to look into defining a context-free grammar for your "language" and using a tool like ANTLR to parse strings using the grammar.
Since all you care for is the valid characters, that's indeed a job for regexes.
A simple way to filter this is just to add letters to the valid characters:
^[A-Z0-9+-/()., ]+$
You can even add a-z if you want to allow lowercase characters as well.

How to check Equal Sign in string using REGEX in C#

I want to check string which look like following
1st radius = 120
and
2nd radius = 'value'
Here is my code
v1 = new Regex(#"^[A-Za-z]+\s[=]\s[A-Za-z]+$");
if (v1.IsMatch(singleLine))`
{
...
...
}
Using #"^[A-Za-z]+\s[=]\s[A-Za-z]+$" this expression 2nd string is matched but not first and when used this #"^[A-Za-z]+\s[=]\s\d{0,3}$" then only matched first one.
And i also want to check for radius = 'val01'
Basing on your effort, it looks as if you were trying to come up with
^[A-Za-z]+\s=\s(?:'[A-Za-z0-9]+'|\d{1,3})$
See the regex demo. Details:
^ - start of string
[A-Za-z]+ - one or more ASCII letters
\s=\s - a = char enclosed with single whitespace chars
(?:'[A-Za-z0-9]+'|\d{1,3}) - a non-capturing group matching either
'[A-Za-z0-9]+' - ', then one or more ASCII letters or digits and then a '
| - or
\d{1,3} - one, two or three digits
$ - end of string (actually, \z is safer when it comes to validating as there can be no final trailing newline after \z, and there can be such a newline after $, but it also depends on how you obtain the input).
If the pattern you tried ^[A-Za-z]+\s[=]\s[A-Za-z]+$ matches the second string radius = 'value', that means that 'value' consists of only chars A-Za-z.
In that case, you could either add matching digits to the second character class:
^[A-Za-z]+\s=\s[A-Za-z0-9]+$
If you either want to match 1-3 digits or at least a single char A-Za-z followed by optional digits:
^[A-Za-z]+\s=\s(?:[0-9]{1,3}|[A-Za-z]+[0-9]*)$
The pattern matches:
^ Start of string
[A-Za-z]+\s=\s Match the first part with chars A-Za-z and the = sign (Note that = does not have to be between square brackets)
(?: Non capture group
[0-9]{1,3} Match 1-3 digits (You can use \d{0,3} but that will also match an emtpy string due to the 0)
| Or
[A-Za-z]+[0-9]* Match 1+ chars A-Za-z followed by optional digits
) Close non capture group
$ End of string
Regex demo

Need value in regex.match group

I want to match regex such that the sign(+ or -) in one group and figure in other group. It may possible that figure comes without any sign(+ or -)
Example
[-] 87.90
[+] 87.78
(-) 87.90
(+) 87.78
89
-89.56
- 89.98
I have used below regular expression
^\W*(\-|\+|)\W*(\d+(\.\d+)?)
By this I am getting empty in group 1
If I use
^\W*(\-|\+)\W*(\d+(\.\d+)?)
then 3rd figure will not match. So in short I want to match figure with (+ or -) or without any sign.
Group 1 is empty because the \W* greedily matches all non-word characters, that is, all parentheses and signs.
You should specify the literal parentheses in the pattern and a character class will be a more natural construct to match either a + or a -:
(?:\(?([-+])\)?)?\p{Zs}*(\d+(\.\d+)?)
See regex demo (if you need a full string match, use ^ at the start and $ at the end of the pattern).
Regex matches:
(?:\(?([-+])\)?)? - an optional non-capturing group ((?:...)) that matches a ( optionally, followed by a plus or minus (Group 1), and then by an optional )
\p{Zs}* - zero or more whitespace symbols
(\d+(\.\d+)?) - (Group 2) one or more digits followed by an optional capturing group (Group 3) that matches a period followed by one or more digits.
Result:

Regex to match pattern "Double-Double;"

I have trouble finding a regex matching this pattern:
A numeric (decimal separator can be . or ,), followed by
a dash -, followed by
a numeric (decimal separator can be . or ,), followed by
a semi-column or a space character
This pattern can be repeated one or more time.
The following examples should match the regex:
1-2;
1-2;3-4;5-6;
1,0-2;
1.0-2;
1,0-2.0;
1-2 3-4;
1-2 3,00-4;5.0-6;
The following examples should not match the regex:
1-2
1 2;
1_2;
1-2;3-4
Edit updated based on moving of 1 2; to non-match.
This should work:
#"^(\d+([,.]\d+)?-\d+([,.]\d+)?[ ;])+(?<=;)$"
Explanation
^ //Start of the string.
( //Start of group to be repeated. You can also use (?=
\d+ //One or more digits.
([,.]\d+)? //With an optional decimal
- //Separated by a dash
\d+([,.]\d+)? //Same as before.
[ ;] //Terminated by a semi-colon or a space
)+ //One or more of these groups.
(?<=;) //The last char before the end needs to be a semi-colon
$ //End of string.
Try this:
#"^([\d.,]+-[\d.,]+[ ;])*[\d.,]+-[\d.,]+;$"
Note that [\d.,]+ accepts some character sequences which wouldn't normally be considered valid "numeric" values such as 00..,.,. You might want to find a better regular expression to match numeric values and substitute it into the regular expression.

Categories