Regex to match pattern "Double-Double;" - c#

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.

Related

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.

Regex to get digits from a string when there is no separator between digits

I have a string like Acc:123-456-789 and another string like -1234567, I need your help to write an expression to match digits in case there is no separator between the digits.
-*(?!\d*(?:\d*-)$)\d*$
Input strings:
Acc:123-456-789 -12323232 7894596
Desired result:
group 1 12323232
group 2 7894596
I think this ought to work:
(?<=^|\s|\s-)(\d+)(?=\s|$)
Breaking it down:
(?<=^|\s|\s-) - A positive lookbehind that matches the start of the string, whitespace, or whitespace followed by a -.
(\d+) - Matches and captures number sequences.
(?=\s|$) - A positive lookahead that matches whitespace or the end of the string.
** Note: If you need to capture negative number sequences, replace (\d+) with (\-?\d+).
Try it online
Regex reference
Remember for use in C# that you need to escape backslashes or use the # prefix to a string literal (#" ").

Regex - Replace specific zeros in a string before a period

sorry for such a direct question but i've spent a little too long trying to find a suitable RegEx that can alter the following strings:
01.10
10.01
setting them as:
1.10
10.1
So basically always remove the first '0' in the complete sequence before each period, or in the last sequence.
Is this possible with RegEx as currently it doesn't seem so?
Try this:
find: (^|\.)0+
replace: $1
See here a demo
Note: if the expression is not at the beginning of the string, you should not use ^, but the word boundary \b, like this:
(\b|\.)0+
eventually, double escape it:
(\\b|\.)0+
See other demo
Perhaps you could try it using this regex. This will not match the zero in 0.0 or 0.1 but only when there are digits after the leading zero(s).
\b0+(?=\d\.\d+\b)|(?<=\b\d+\.)0+(?=\d+\b)
\b word boundary
0+(?=\d\.\d+\b) match a zero and use a positive lookahead to assert that the zero is followed by a digit, dot, one or more digits and a word boundary
| Or
(?<=\b\d+\.)0+(?=\d+\b) Positive lookbehind that asserts that what is on the left is a wordboundary, one or more digits and a dot. Then match one or more zeroes and assert that what follows id one or more digits and a wordboundary.

Regex to insert and replace characters in a string C#

I have a string which looks like this :-
"$.ConfigSettings.DatabaseSettings.DatabaseConnections.SqlConnectionString.0.Id"
and I want the result to look like this :-
"$.ConfigSettings.DatabaseSettings.DatabaseConnections.SqlConnectionString[0].Id"
Basically wherever there is a single digit preceded and succeeded by a period I need to change it to [digit] followed by period ie [digit]. .I have seen tons of examples where people are only replacing the regex string.
How will I do this using Regex.Replace in C#
Regex.Replace(input, #"\.(\d)(?=\.)", "[$1]")
\. - capture a "."
(\d) - then a single digit in a capturing group ($1 in the replacement)
(?= - start a positive lookahead
\. - that matches a "."
) - end the lookahead
So, it means : (match a dot followed by a digit in a capturing group) only if it is followed by a dot
So we matched ".0" and captured "0". We replace the entire match with "[$1]", where $1 refers to the first captured group.
See "Grouping Constructs in Regular Expressions" : https://msdn.microsoft.com/en-us/library/bs2twtah(v=vs.110).aspx for information about the different grouping constructs that I use in this solution.

C# Regex match on special characters

I know this stuff has been talked about a lot, but I'm having a problem trying to match the following...
Example input: "test test 310-315"
I need a regex expression that recognizes a number followed by a dash, and returns 310. How do I include the dash in the regex expression though. So the final match result would be: "310".
Thanks a lot - kcross
EDIT: Also, how would I do the same thing but with the dash preceding, but also take into account that the number following the dash could be a negative number... didnt think of this one when I wrote the question immediately. for example: "test test 310--315" returns -315 and "test 310-315" returns 315.
Regex regex = new Regex(#"\d+(?=\-)");
\d+ - Looks for one or more digits
(?=\-) - Makes sure it is followed by a dash
The # just eliminates the need to escape the backslashes to keep the compiler happy.
Also, you may want this instead:
\d+(?=\-\d+)
This will check for a one or more numbers, followed by a dash, followed by one or more numbers, but only match the first set.
In response to your comment, here's a regex that will check for a number following a -, while accounting for potential negative (-) numbers:
Regex regex = new Regex(#"(?<=\-)\-?\d+");
(?<=\-) - Negative lookbehind which will check and make sure there is a preceding -
\-? - Checks for either zero or one dashes
\d+ - One or more digits
(?'number'\d+)- will work ( no need to escape ). In this example the group containing the single number is the named group 'number'.
if you want to match both groups with optional sign try:
#"(?'first'-?\d+)-(?'second'-?\d+)"
See it working here.
Just to describe, nothing complicated, just using -? to match an optional - and \d+ to match one or more digit. a literal - match itself.
here's some documentation that I use:
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
in the comments section of that page, it suggests escaping the dash with '\-'
make sure you escape your escape character \
You would escape the special meaning of - in regex language (means range) using a backslash (\). Since backslash has a special meaning in C# literals to escape quotes or be part of some characters, you need to escape that with another backslash(\). So essentially it would be \d+\\-.
\b\d*(?=\-) you will want to look ahead for the dash
\b = is start at a word boundry
\d = match any decimal digit
* = match the previous as many times as needed
(?=\-) = look ahead for the dash
Edited for Formatting issue with the slash not showing after posting

Categories