Regular Expression for negative accounting money values? - c#

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

Related

Regex to check time period value 1W, 5Y

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

regex to accept only numbers, hypens, plus sign and space

I need to create an regex that accepts only numbers, hypens (-), plus sign (+), spaces along with round brackets ( ).
I tried the regex (?\+?\d+)?[-.\s]?\d+[-.\s]?\d+.
What is need is like (123) 123-123-123, (+91) 123-123-1234 or 1234567890, or 123 123 1234 or +92 345 345 3456 or +92 123-123-1234.
Your regex starts with (?\+?\d+)? which is not correct. Maybe you were looking for (?:\+?\d+)?[-.\s]?\d+[-.\s]?\d+ which makes the first group non capturing.
If you want to match only numbers, hyphens (-), plus sign (+), spaces along with round brackets ( ) one or more times (so ++++ would also match) you could use:
^[-+\d() ]+$
If you want to narrow down your match for your provided strings maybe this could be an option.
There are 2 parts:
The optional part at the beginning (123) or (+91) or +92
A whitespace followed by either a format like 123-123-123 with hyphens or without hyphens 123 123 1234
^(?:\+\d+|\(\+?\d+\))?(?:\d{10}| ?\d{3}([ -])\d{3}\1\d{3,4})$
Explanation
From the beginning of the string ^
A non capturing group (?:
Which matches either a plus sign and one or more digits \+\d+
Or |
Match parenthesis with an optional plus sign, one or more digits and parenthesis \(\+?\d+\)
Close the non capturing group and make it optional )?
A non capturing group (?:
Which matches either 10 digits \d+{10}
Or |
Optional whitespace and digits pattern ?\d{3}([ -])\d{3}\1\d{3,4} which uses a capturing group \1 for the whitespace or the hyphen.
Close the non capturing group )
The end of the string $

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:

C# Regex Match 15 Characters, Single Spaces, Alpha-Numeric

I need to match a string under the following conditions using Regex in C#:
Entire string can only be alphanumeric (including spaces).
Must be a maximum of 15 characters or less (including spaces).
First & last characters can only be a letter.
A single space can appear multiple times in anywhere but the first and last characters of the string. (Multiple spaces together should not be allowed).
Capitalization should be ignored.
Should match the WHOLE word(s).
If any one of these preconditions are broken, a match should not follow.
Here is what i currently have:
^\b([A-z]{1})(([A-z0-9 ])*([A-z]{1}))?\b$
And here are some test strings that should match:
Stack OverFlow
Iamthe greatest
A
superman23s
One Two Three
And some that shouldn't match (note the spaces):
Stack [double_space] Overflow Rocks
23Hello
ThisIsOver15CharactersLong
Hello23
[space_here]hey
etc.
Any suggestions would be much appreciated.
You should use lookaheads
|->matches if all the lookaheads are true
--
^(?=[a-zA-Z]([a-zA-Z\d\s]+[a-zA-Z])?$)(?=.{1,15}$)(?!.*\s{2,}).*$
-------------------------------------- ---------- ----------
| | |->checks if there are no two or more space occuring
| |->checks if the string is between 1 to 15 chars
|->checks if the string starts with alphabet followed by 1 to many requireds chars and that ends with a char that is not space
you can try it here
Try this regex: -
"^([a-zA-Z]([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])$"
Explanation : -
[a-zA-Z] // Match first character letter
( // Capture group
[ ](?=[a-zA-Z0-9]) // Match either a `space followed by non-whitespace` (Avoid double space, but accept single whitespace)
| // or
[a-zA-Z0-9] // just `non-whitespace` characters
){0,13} // from `0 to 13` character in length
[a-zA-Z] // Match last character letter
Update : -
To handle single characters, you can make the pattern after 1st character optional as nicely pointed by #Rawling in comments: -
"^([a-zA-Z](([ ](?=[a-zA-Z0-9])|[a-zA-Z0-9]){0,13}[a-zA-Z])?)$"
^^^ ^^^
use a capture group make it optional
And my version, again using look-aheads:
^(?=.{1,15}$)(?=^[A-Z].*)(?=.*[A-Z]$)(?![ ]{2})[A-Z0-9 ]+$
explained:
^ start of string
(?=.{1,15}$) positive look-ahead: must be between 1 and 15 chars
(?=^[A-Z].*) positive look-ahead: initial char must be alpha
(?=.*[A-Z]$) positive look-ahead: last char must be alpha
(?![ ]{2}) negative look-ahead: string mustn't contain 2 or more consecutive spaces
[A-Z0-9 ]+ if all the look-aheads agree, select only alpha-numeric chars + space
$ end of string
This will also need the IgnoreCase option setting

Regex to clean repetitions of characters

I have a pattern in the string like this:
T T and I want to T
And It can be any character from [a-z].
I have tried this Regex Example but not able to replace it.
EDIT
Like I have A Aa ar r then it should become Aar means replace any character 1st or 2nd no matter what it is.
You can use the backreferences for this.
/([a-z])\s*\1\s?/gi
Example
Some more explanation:
( begin matching group 1
[a-z] match any character from a to z
) end matching group 1
\s* match any amount of space characters
\1 match the result of matching group 1
exactly as it was again
this allows for the repition
\s? match none or one space character
this will allow to remove multiple
spaces when replacing

Categories