Custom regular expression [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi I need to match this format
N - Number
NN,NN
or
NN.NN
also
N,N and N.N
and combinations
N.NN and N,NN or NN,N and NN.N

Here's your regex:
\d{1,2}[.,]\d{1,2}
See it here in action: http://regexr.com?2vman
Here's a slightly different version:
\d\d?[.,]\d\d?
See it here in action: http://regexr.com?2vmaq
If you want to also match with out a decimal, use this:
\d\d?[.,]?\d{0,2}
See it here in action: http://regexr.com?2vml4

How about:
\d{1,2}(?:[.,]\d{1,2})?
explanation:
\d{1,2} : one or two digits
(?: : start non capture group
[.,] : . or ,
\d{1,2} : one or two digits
)? : end group, optional

Why match it?
Just remove the commas and use the actual number:
Regex.Replace("8,675,309.02", "(,)", string.Empty) // Outputs 8675309.02
If this is a validation scenario, using int.Parse will let you know if its valid.

I would go with something like this:
Regex regex = new Regex(#"\d{1,2}[\.,]\d{1,2}");

Related

How to match 4 character code to actual words using regular expressions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Having codes in the following format [Alphanumeric][Letter][Alphanumeric][Alphanumeric] i.e A1AA
Now I also have a dictionary of all the 4 letter words I am trying to block i.e R2D2
What I am looking for is a regular expression to find the matches for each code and item in my dictionary but also 1 step further and to replace characters and letters which look alike i.e. i and 1, s and 5 and see if any matches happen there.
Anything like that out there
Let's say your dictionnary contains this codes:
R2D2
C3P0
X5ZZ
I would load the dictionnary and build on the fly a regex. The final regex would be:
(?-i)(R2D2|C3P0|X5ZZ)
Then apply this regex to each of your codes
If Regex.Matches(finalRegex) Then
// Evil code catched
Else
// Nice code found ...
End If
I'll give you a headstart
var matches = Regex.Matches(#"A1AA", #"([a-zA-Z0-9][a-zA-Z][a-zA-Z0-9][a-zA-Z0-9])");
foreach(Match match in matches)
{
Console.WriteLine(match.Groups[1].Value);
}
This part of the Regex code [a-zA-Z0-9] will capture any character which is alphanumeric.
You can then do a foreach loop on the matches against a dictionary of words.

Regex for Currency with ',' and/or '.' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm kinda rusty with regular expressions. I need a REGEX that will validate values formatted like the following:
123.00
123,00
1324,00
1234.00
123
1213.0
I tried ^\d.\d{2}$, but it does not seem to match all values.
Appreciate any assistance.
You can use the following:
\d+[.,]?\d+
Good luck!
\d+[,.]?\d*
I would strongly advise against mixing cultures especially for persistence or transport.
The Regex you're likely looking for is something like #"\d+([,.]\d+)?"
It specifies "Some number of digits, optionally followed by a . or , and at least one digit". It would not match 123..
If you want to match culture-specific strings, however, I'd recommend using NumberFormatInfo.CurrencyDecimalSeparator and then look for that specifically.

Regex to remove consecutive special characters greater than specified count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to remove all appearances of the character / and \ in a string if it appears consecutively more than twice, using Regex. This means, if a string contains abc////////////////////////def, I would like to get all / removed. However, it should not remove "the / in http://.
Could someone please suggest?
You can use /{3,}, which will match 3 or more occurrences of the / character.
var result = Regex.Replace("abc///def", "/{3,}", "");
Update: to reply to your comment, the * character is a metacharacter in regex, which holds a special meaning, so you need to escape it. Try this: \*{3,}. If you want to combine both characters, you can use a character class: [/*]{3,}. A character class is denoted by the square brackets. Inside a character class you don't need to escape metacharacters, which is why I simply list * inside without escaping it as I did earlier.
Use negative look-behind assertion:
#"(?<!https?:)/{2,}|\\{2,}"
For example:
Regex pattern = new Regex(#"(?<!https?:)/{2,}|\\{2,}");
Console.WriteLine(pattern.Replace(#"http://example.com", ""));
Console.WriteLine(pattern.Replace(#"abc//////////def", ""));
Console.WriteLine(pattern.Replace(#"abc\\\\\\\\\\def", ""));
prints
http://example.com
abcdef
abcdef

Single digit followed by two alphabets regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to regular expression. I need a regular expression to validate single digit within range of [1-5] followed by alphabet [A-C] and followed by optional alphabet [X-Z]
1A (Valid)
2BZ (Valid)
7B (Invalid)
What you tried is right, except you're missing the start and end characters, so it's succeeding on a partial match.
You need:
^[1-5][a-cA-C][x-zX-Z]?$
Use online testers to validate it:
[1-5]+[A-C]+[X-Z]*
regexpal
gskinner

Can someone explain this Regex to me [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I found this regex in working C# code. Can someone explain it to me please :
#"(?s)\{\{\s*" + templateTitle + #"(.*?)}}\r?\n?"
I tested it and it returns text like
{{ templateTitleValue anything}}
What I don't understand is the two '}}' that are not escaped by \ like the two '}}'. And what is '(?s)' in the beginning.
Thanks
Actually, you don't need to escape the opening braces either. It's clear from context that they are not quantifiers (as in X{1,3} which would match X, XX or XXX). But it's good practice to escape them anyway (and to also escape the closing ones), just in case the regex changes into something where there could be some ambiguity.
(?s) means "allow the dot (.) to match all characters, including newlines". This is also known as the Singleline or DOTALL option.

Categories

Resources