Regular expression for NN-ARID-NNN? - c#

What will be the RegularExression for this?
NN-ARID-NNN?
//N = Number
I have tried this ^[0-9/-0-9]+$

You're not matching the ARID at all and a character class will match in any order... You might want to use something more like this:
^[0-9]{2}-ARID-[0-9]{3}$
[Assuming that ? is not in the actual string...]
If you want the first two digits to be within the range of 00 to 13, then you can use the OR operator with | and a group:
^(?:0[0-9]|1[0-3])-ARID-[0-9]{3}$
^^^ ^ ^
| OR |
| |
+---- Group ---+
Breakdown:
^ Matches beginning of string
(?: Beginning of group
0[0-9] Matches 00 to 09 only
| OR
1[0-3] Matches 10 to 13 only
) End of group
-ARID- Matches -ARID- literally
[0-9]{3} Matches 3 digits
$ Matches end of line
When there is an option of matching 00-09 or 10-13, the pattern just cannot match a blank. There's no way it will match if the numbers are not there.

Related

C# Regex matching two numbers divided by specific string and surrounded by brackets

I'm looking for a .NET Regex pattern that matches the following:
string starts with the [ character
followed by an integer or decimal number
followed by .. (space character, dot, dot, space character)
followed by an integer or decimal number
followed by the last character of the string which is )
*- the decimal numbers have a decimal separator, the . character
*- the integer numbers or the integer value of the decimal numbers should have a maximum of 4 digits
*- the decimal numbers should have a maximum of 4 fractional digits
*- the numbers can be negative
*- if a number is positive then the + sign is missing
*- doesn't matter which one of the two numbers is smaller (first number can be bigger than the second one, "[56 .. 55)" for instance)
The pattern should match the following:
"[10 .. 15)"
"[100 .. 15.2)"
"[10.431 .. 15)"
"[-10.3 .. -5)"
"[-10.4 .. 5.12)"
"[10.4312 .. -5.1232)"
I'd also like to obtain the 2 numbers as strings from the string in case the pattern matches:
obtain "10" and "15" from "[10 .. 15)"
obtain "-10.4" and "5.12" from "[-10.4 .. 5.12)"
The following regex should be fine.
^\[-?\d+(?:\.\d+)? \.\. -?\d+(?:\.\d+)?\)$
var pattern = #"^\[-?\d+(?:\.\d+)? \.\. -?\d+(?:\.\d+)?\)$";
var inputs = new[]{"[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", };
foreach (var input in inputs)
{
Console.WriteLine(input + " = " + Regex.IsMatch(input, pattern));
}
// [10 .. 15) = True
// [100 .. 15.2) = True
// [10.431 .. 15) = True
// [-10.3 .. -5) = True
// [-10.4 .. 5.12) = True
// [10.4312 .. -5.1232) = True
https://dotnetfiddle.net/LpswtI
You can use
^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$
See the regex demo. Details:
^ - start of string
\[ - a [ char
(-?\d{1,4}(?:\.\d{1,4})?) - Group 1: an optional -, one to four digits and then an optional sequence of a . and one to four digits
\.\. - a .. string
(-?\d{1,4}(?:\.\d{1,4})?) - Group 2: an optional -, one to four digits and then an optional sequence of a . and one to four digits
\) - a ) char
$ - end of string (use \z if you need to check for the very end of string).
See the C# demo:
var texts = new List<string> { "[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", "[12345.1234 .. 0)", "[1.23456 .. 0" };
var pattern = new Regex(#"^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$");
foreach (var s in texts)
{
Console.WriteLine($"---- {s} ----");
var match = pattern.Match(s);
if (match.Success)
{
Console.WriteLine($"Group 1: {match.Groups[1].Value}, Group 2: {match.Groups[2].Value}");
}
else
{
Console.WriteLine($"No match found in '{s}'.");
}
}
Output:
---- [10 .. 15) ----
Group 1: 10, Group 2: 15
---- [100 .. 15.2) ----
Group 1: 100, Group 2: 15.2
---- [10.431 .. 15) ----
Group 1: 10.431, Group 2: 15
---- [-10.3 .. -5) ----
Group 1: -10.3, Group 2: -5
---- [-10.4 .. 5.12) ----
Group 1: -10.4, Group 2: 5.12
---- [10.4312 .. -5.1232) ----
Group 1: 10.4312, Group 2: -5.1232
---- [12345.1234 .. 0) ----
No match found in '[12345.1234 .. 0)'.
---- [1.23456 .. 0 ----
No match found in '[1.23456 .. 0'.
This works (see this .Net Fiddle:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
Match m = rx.Match("[123 .. -9876.5432]");
if (!m.Success )
{
Console.WriteLine("No Match");
}
else
{
Console.WriteLine(#"left: {0}", m.Groups[ "left" ] );
Console.WriteLine(#"right: {0}", m.Groups[ "right" ] );
}
}
private static readonly Regex rx = new Regex(#"
^ # anchor match at start-of-text
[[] # a left square bracket followed by
(?<left> # a named capturing group, containing a number, consisting of
-?[0-9]{1,4} # - a mandatory integer portion followed by
([.][0-9]{1,4})? # - an optional fractional portion
) # the whole of which is followed by
[ ][.][.][ ] # a separator (' .. '), followed by
(?<right> # another named capturing group containing a number, consisting of
-?[0-9]{1,4} # - a mandatory integer portion followed by
([.][0-9]{1,4})? # - an optional fractional portion
) # the whole of which is followed by
\] # a right square bracket, followed by
$ # end-of-text
",
RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture
);
}

Latitude/Longitude RegEx in N-S and E-W format

I'm asking what looks like a similar question to all the lat/lon regex questions, but my question puts a different spin on the format that I haven't been able to find. I want to only accept a format as such:
LAT: XX-XX.XX N|S
LON: XXX-XX.XX E|W
This is for a C# window text entry where latitude and longitude are entered in separate textboxes.
I want the format to only accept 1 dash(-) and 1 decimal in those locations (i.e. negative values are invalid) and enforce the range correctly so that all place values need to be entered such as:
LAT 0-90 North or South
00-00.00 N valid
5-00.00 N invalid
05-00.00 N valid
90-00.00 N valid
89-59.99 S valid
90-60.00 S invalid
91.00.00 N invalid
LON 0-180 East or West
0-0.0 E invalid
15-00.00 E invalid
015.00.00 E valid
180-00.00 E valid
180-01.00 E invalid
179-59.99 W valid
179-60.00 W invalid
181-00.00 W invalid
I know how to do it digit by digit such as for Latitude:
[0-9][0-9]-[0-5][0-9].[0-9][0-9] [N|S]
That is the extent of my knowledge of RegEx authoring.
As always, any help on this would be much appreciated.
My suggestion is
String patternLatitude = #"^(90\-00\.00)|([0-8]\d\-[0-5]\d\.\d\d) (N|S)$";
String patternLongitude = #"^(180\-00\.00)|((1[0-7]\d)|(0\d\d)\-[0-5]\d\.\d\d) (W|E)$";
providing that the given example
015.00.00 E valid
should be actually invalid. More testing examples (all invalid)
090-00.01 N
180-00.01 E
190-00.00 E
200-00.00 E
Explanation:
Latitude:
90-00.00 is a special case (the only possible value with 90 degree), for other degree values we can put down [0..8]\d; minutes are [0..5]\d and decimals are just two digits: \d\d.
Longitude: 180-00.00 is a special case (the only possibility with 180 degree); the second case 1** lattitudes: since we don't have 180 or 190 lattitudes we can put them as 1[0-7]\d; finally if a lattitude starts with 0 it can have any two digits more: 0\d\d. Minutes and their decimals are the same as they are in the Lattitude case.
The following will give you a regex to match the latitudes, along with capturing groups to enable access to the degs/mins/secs and N/S values:
(([0-8]\d)[-.]([0-5]\d)\.(\d\d)|(90[-.]00\.00)) ([N|S])
And the same for E/W:
((0\d\d|1[0-7]\d)[-.]([0-5]\d)\.(\d\d)|(180[-.]00\.00)) ([E|W])
This may be hard to look at ... but it validates.
# #"(?:(?:(?:(?:0\d|[1-8]\d)(?=-\d\d\.\d\d[ ][NS])|(?:0\d\d|1[0-7]\d)(?=-\d\d\.\d\d[ ][EW]))-(?:(?:(?:0\d|[1-5]\d)\.\d\d)))|(?:(?:90(?=-\d\d\.\d\d[ ][NS])|180(?=-\d\d\.\d\d[ ][EW]))-00\.00))[ ][NSEW]"
(?: #
(?: # =============
(?: #
(?: # LAT: 00 to 89 North or South
0 \d #
| [1-8] \d #
) #
(?= - \d\d \. \d\d [ ] [NS] )
| # or,
(?: # LON: 000 to 179 East or West
0 \d\d #
| 1 [0-7] \d #
) #
(?= - \d\d \. \d\d [ ] [EW] )
) #
#
- # -
#
(?: #
(?: #
(?: # 00 to 59
0 \d #
| [1-5] \d #
) #
\. # .
\d\d # 00 to 99
) #
) #
) #
| # or,
(?: # =============
(?: #
90 # LAT: 90 North or South
(?= - \d\d \. \d\d [ ] [NS] )
| # or,
180 # LON: 180 East or West
(?= - \d\d \. \d\d [ ] [EW] )
) #
- 00 \. 00 # - 00.00
) #
) #
[ ] # =============
[NSEW] # N,S,E,W
To limit degrees to 0-90/0-180, and seconds to 0-0/59-99, I would go for these regex:
#Latitude:
(([0-8]\d)-(0\d|[1-5]\d)\.\d\d|90-00.00)\s[NS]
#Longitude:
((0\d\d|1[0-7]\d)-(0\d|[1-5]\d)\.\d\d|180-00.00)\s[EW]

How to create Regex that contains Not colon char?

I have created a regular expression that seems to be working somewhat:
// look for years starting with 19 or 20 followed by two digits surrounded by spaces.
// Instead of ending space, the year may be followed by a '.' or ';'
static Regex regex = new Regex(#" 19\d{2} | 19\d{2}. | 19\d{2}; | 20\d {2} | 20\d{2}. | 20\d{2}; ");
// Trying to add 'NOT followed by a colon'
static Regex regex = new Regex(#" 19\d{2}(?!:) | 19\d{2}. | 19\d{2}; | 20\d{2}(?!:) | 20\d{2}. | 20\d{2}; ");
// Trying to optimize --
//static Regex regex = new Regex(#" (19|20)\d{2}['.',';']");
You can see where I tried to optimize a bit.
But more importantly, it is finding a match for 2002:
How do I make it not do that?
I think I am looking for some sort of NOT operator?
(?:19|20)\d{2}(?=[ ,;.])
Try this.See demo.
https://regex101.com/r/sJ9gM7/103
I'd rather go with \b here, this will help deal with other punctuation that may appear after/before the years:
\b(?:19|20)[0-9]{2}\b
C#:
static Regex regex = new Regex(#"\b(?:19|20)[0-9]{2}\b");
Tested in Expresso:
Problem in your regex is dot.
You should have something like this:
static Regex regex =
new Regex(#" 19\d{2} | 19\d{2}[.] | 19\d{2}; | 20\d{2} | 20\d{2}[.] | 20\d{2}; ");
This did it for me:
// look for years starting with 19 or 20 followed by two digits surrounded by spaces.
// Instead of ending space, the year may also be followed by a '.' or ';'
// but may not be followed by a colon, dash or any other unspecified character.
// optimized --
static Regex regex = new Regex(#"(19|20)\d{2} | (19|20)\d{2};| (19|20)\d{2}[.]");
Used Regex Tester here:
http://regexhero.net/tester/

Percentage Regex with comma

I have this RegEx for C# ASP.NET MVC3 Model validation:
[RegularExpression(#"[0-9]*\,?[0-9]?[0-9]")]
This works for almost all cases, except if the number is bigger than 100.
Any number greater than 100 should show error.
I already tried use [Range], but it doesn't work with commas.
Valid: 0 / 0,0 / 0,00 - 100 / 100,0 / 100,00.
Invalid (Number > 100).
Not sure if zero's are only optional digits at the end but
# (?:100(?:,0{1,2})?|[0-9]{1,2}(?:,[0-9]{1,2})?)
(?:
100
(?: , 0{1,2} )?
|
[0-9]{1,2}
(?: , [0-9]{1,2} )?
)
Zero's only option at end
# (?:100|[0-9]{1,2})(?:,0{1,2})?
(?:
100
| [0-9]{1,2}
)
(?: , 0{1,2} )?
And, the permutations for no leading zero's except for zero itself
# (?:100(?:,0{1,2})?|(?:0|[1-9][0-9]?)(?:,[0-9]{1,2})?)
(?:
100
(?: , 0{1,2} )?
|
(?:
0
|
[1-9] [0-9]?
)
(?: , [0-9]{1,2} )?
)
# (?:100|0|[1-9][0-9])(?:,0{1,2})?
(?:
100
|
0
|
[1-9] [0-9]
)
(?: , 0{1,2} )?
Here's a RegEx that matches your criteria:
^(?:(?:[0-9]|[1-9]{1,2})(?:,[0-9]{1,2})?|(?:100)(?:,0{1,2})?)$
(Given your use case, I have assumed that your character sequence appears by itself and is not embedded within other content. Please let me know if that is not the case.)
And here's a Perl program that demonstrates that RegEx on a sample data set. (Also see live demo.)
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
chomp;
# A1 => An integer between 1 and 99, without leading zeros.
# (Although zero can appear by itself.)
#
# A2 => A optional fractional component that may contain no more
# than two digits.
#
# -OR-
#
# B1 => The integer 100.
#
# B2 => A optional fractional component following that may
# consist of one or two zeros only.
#
if (/^(?:(?:[0-9]|[1-9]{1,2})(?:,[0-9]{1,2})?|(?:100)(?:,0{1,2})?)$/) {
# ^^^^^^^^A1^^^^^^ ^^^^^A2^^^^ ^B1 ^^^B2^^
print "* [$_]\n";
} else {
print " [$_]\n";
}
}
__DATA__
0
01
11
99
100
101
0,0
0,00
01,00
0,000
99,00
99,99
100,0
100,00
100,000
100,01
100,99
101,00
Expected Output
* [0]
[01]
* [11]
* [99]
* [100]
[101]
* [0,0]
* [0,00]
[01,00]
[0,000]
* [99,00]
* [99,99]
* [100,0]
* [100,00]
[100,000]
[100,01]
[100,99]
[101,00]

C# Regex Grouping

I have to write a function that will get a string and it will have 2 forms:
XX..X,YY..Y where XX..X are max 4 characters and YY..Y are max 26 characters(X and Y are digits or A or B)
XX..X where XX..X are max 8 characters (X is digit or A or B)
e.g. 12A,784B52 or 4453AB
How can i user Regex grouping to match this behavior?
Thanks.
p.s. sorry if this is to localized
You can use named captures for this:
Regex regexObj = new Regex(
#"\b # Match a word boundary
(?: # Either match
(?<X>[AB\d]{1,4}) # 1-4 characters --> group X
, # comma
(?<Y>[AB\d]{1,26}) # 1-26 characters --> group Y
| # or
(?<X>[AB\d]{1,8}) # 1-8 characters --> group X
) # End of alternation
\b # Match a word boundary",
RegexOptions.IgnorePatternWhitespace);
X = regexObj.Match(subjectString).Groups["X"].Value;
Y = regexObj.Match(subjectString).Groups["Y"].Value;
I don't know what happens if there is no group Y, perhaps you might need to wrap the last line in an if statement.

Categories

Resources