i am trying to write a regular expression to validate the numbers with only one space in undefined places?
Maximum of 12 characters with one space or Maximum of 11 characters without spaces.
Ex: '25897 569874','5674','65783987665','435 6523'
i have tried with ^[0-9]{0,12}$.this is not perfect cause I don't know how to place the spaces and its counts.
You can use this regex:
^(?:\d{1,11}|(?=\d+ \d+$)[\d ]{3,12})$
\d{1,11} will match from 1 to 11 digits without space.
(?=\d+ \d+$)[\d ]{3,12} will match up to 11 digits with one space somewhere in the middle. The space cannot be leading or trailing, so ' 23' will be rejected.
(?=\d+ \d+$) is a look-ahead that matched one or more digit, then a space, then one or more digit, then anchor the end of string. It guarantees only one space will appear and the space will not be leading or trailing. The look-ahead also implicitly confirms that there are at least 3 characters in the string.
[\d ]{3,12} will guarantee the string only contains digits or space, and up to 12 of them. The lower bound of number of repetition can be set to 3 or lower, since it has been implied by the look-ahead.
The 2 constraints together guarantees that text contains from 1 to 11 digits and an optional space at arbitrary position in between the digits.
To allow leading space, but reject single space, empty string and trailing spaces:
^(?:\d{1,11}|(?=\d* \d+$)[\d ]{2,12})$
Again, the look-ahead implies at least 2 characters, so the number of repetitions can be set to 2 or lower.
^[0-9 ]{0,12}$ will match upto 12 character string with or without space
If you need multiple criteria,
try OR operator (pipe): |
^[0-9 ]{0,12}$|another condition
Related
I have a bunch of strings that may contains certain patterns. Specifically, the following 3.
Starts with (- followed by 10 digits followed by ).
E.g.:
(-1234567890)
Starts with (, ends with ), and may contain 1 or more characters, but NO spaces.
E.g.:
(ABC) or (AF33) or (2345)
Starts with (, ends with ), and may contain 1 or more characters, INCLUDING spaces.
E.g.:
(Some string)
The strings I work with may contain zero or more of the patterns above. My requirement is to match ONLY the second one from above in a given string, and I'd like to be able to use Regex class in C#.
For example, let's say following are five different strings I have.
This is some random text.
This is some (ABC) random (-1234567890) text.
This is some (XY12) random (-1234567890) text.
This is some (Contains space) random (-1234567890) text.
This is some () random text.
My Regex should match only the 2nd and 3rd strings from the above list.
So far, I've managed to write this following Regex, which excludes strings 1 and 5.
.*\((?!\-).+\).*
This matches 2nd, 3rd, AND 4th strings above. Now I'm not sure how I can get it to exclude the 4th, one which contains spaces inside parenthesis. I know that \S detects whitespaces, but how can I tell it to detect strings that do not contain spaces only within the parenthesis that don't contain a - after the first (?
EDIT 1:
There will never be nested parenthesis in my strings.
EDIT 2:
Here's a Regex Tester.
.*\(\w+\).*
If you use above regex, second and third strings are matches only
.* all characters
( pharantesis
\w+ all word characters (at least one)
) pharantesis
.* all characters
\(([^- ]+[^ ]*)\)
should work
Explanation:
[^- ]+ will first match one character that's neither - or This will make sure it contains at least one character
Then [^ ]* will match 0 or more none white space characters
This will work for any char set
What I would like to do is to take input string, in architectural format and convert it to a double in inches.
For example:
Input: (String) Output: (Double)
1'-2" 14
1'-2 1/2" 14.5
1'2 3/16" 14.1875
1' 12
12 12
12" 12
1'0.5 12.5
1'0.5" 12.5
1'-0.5 12.5
1'-0.5" 12.5
I know I would need to iterate through every character in the string and test a bunch of cases but I did not know if there was some built in function within c# or within some other resource that could do this for me and not make me re-invent the wheel.
Regex for the win!
Okay, if you're new to Regex, it's basically a way of parsing strings. So, realistically, what does your input consist of?
At a high level, you've got one of these three possibilities:
Composite: Number, followed by ', followed by either a - or space,
followed by a number, and optionally ending with a "
Feet Only: Number, followed by a '
Inches Only: Number, optionally followed by a "
And those 'Number's?
Possibilities:
1+ digits (aka, "23")
1+ digits, a '.', and 1+ digits (aka, "32.43")
1+ digits, a space, 1+ digits, a slash, and 1+ digits (aka, "32
13/16")
1+ digits, a slash, and 1+ digits (aka, "13/16")
Okay, so first up, we need a regex for one of your "numbers":
\d+|\d+.\d+|\d+ \d+\/\d+|\d+\/\d+
(Looks complicated, but see these two pages for reference: http://www.rexegg.com/regex-quickstart.html and https://regex101.com/)
Now, just so our regex'es don't get too complicated, you could do something like this:
string regexSnippetForNumber = #"\d+|\d+.\d+|\d+ \d+\/\d+|\d+\/\d+";
string regexForComposite =
"^(" + regexSnippetForNumber + ")'[ -]" +
"(" + regexSnippetForNumber + ")\"?$"
... and then, if the input matches regexForComposite, you use the two capturing groups to get the two numbers. (Which you'd have to parse to get the numerical value.)
Hopefully that makes sense and can get you close enough to the finish line. If you've never used Regexes before, I highly suggest you read up on them. They're incredibly handy when you need to do string parsing that can otherwise be really annoying (like this exact problem!)
I'm trying to write a regular expression using C#/.Net that matches 1-4 alphanumerics followed by spaces, followed by 10 digits. The catch is the number of spaces plus the number of alphanumerics must equal 4, and the spaces must follow the alphanumerics, not be interspersed.
I'm at a total loss as to how to do this. I can do ^[A-Za-z\d\s]{1,4}[\d]{10}$, but that lets the spaces fall anywhere in the first four characters. Or I could do ^[A-Za-z\d]{1,4}[\s]{0,3}[\d]{10}$ to keep the spaces together, but that would allow more than a total of four characters before the 10 digit number.
Valid:
A12B1234567890
AB1 1234567890
AB 1234567890
Invalid:
AB1 1234567890 (more than 4 characters before the numbers)
A1B1234567890 (less than 4 characters before the numbers)
A1 B1234567890 (space amidst the first 4 characters instead of at the end)
You can force the check with a look-behind (?<=^[\p{L}\d\s]{4}) that will ensure there are four allowed characters before the 10-digits number:
^[\p{L}\d]{1,4}\s{0,3}(?<=^[\p{L}\d\s]{4})\d{10}$
^^^^^^^^^^^^^^^^^^^^
See demo
If you do not plan to support all Unicode letters, just replace \p{L} with [a-z] and use RegexOptions.IgnoreCase.
Here's the regex you need:
^(?=[A-Za-z0-9 ]{4}\d{10}$)[A-Za-z0-9]{1,4} *\d{10}$
It uses a lookahead (?= ) to test if it's followed by 4 chars, either alnum or space, and then it goes back to where it was (the beggining of string, not consuming any chars).
Once that condition is met, the rest is a expression quite similar to what you were trying ([A-Za-z0-9]{1,4} *\d{10}).
Online tester
I know this is dumb, but must work exactly as required.
^[A-Za-z\d]([A-Za-z\d]{3}|[A-Za-z\d]{2}\s|[A-Za-z\d]\s{2}|\s{3})[\d]{10}$
Not sure what you are looking for, but perhaps:
^(?=.{14}$)[A-Za-z0-9]{1,4} *\d{10}
demo
Try this:
Doesn't allow char/space/char combination and starts with a char:
/\b(?!\w\s{1,2}\w+)\w(\w|\s){3}\d{10}/gm
https://regex101.com/r/fF2tR8/2
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
Is it possible to create a 'dynamic' discount mask that takes % or numbers as discount values? What is the simple way to do this?
the samples of valide input: -25% or 0.25 or -5$ not 0 and two digit after dot
Try
#"(\+|-)?(\d+(\.\d*)?|\.\d+)%?"
It will find:
123.23
12.4%
.34
.34%
45.
45.%
8
7%
34
34%
+2.55%
-1.75%
UPDATE
and with ...
#"(\+|-)?(\d+(,\d{3})*(?!\d)(\.\d*)?|\.\d+)%?"
... you can include thousands separators as well.
I must confess that my second regex expression looks like a cat had walked accross my keyboard. Here the explanation
(\+|-)? optionally ? a plus or a minus sign.
\d+(,\d{3})*(?!\d)(\.\d*)? one or more digits \d+ followed by any number of thousands separators plus three digits (,\d{3})*, not followed by any digit (?!\d) in order to disallow four digits in sequence, optionally followed by a decimal point and any number of digits (\.\d*)?.
|\.\d+ or alternatively a decimal point followed by at least one digit.
%? finally an optional percent sign.
If I understand your question right, you want something like this:
#"^[+-]?(?:\d*\.)?\d+[%$]?$"
That's partly based on your example of -5$. Usually, though, the $ would go in front, so you'd want something like:
#"^(?:\$(?!.*%))?[+-]?(?:\d*\.)?\d+%?$"
That would allow $-5.00, 10, or +20%, but block $5%.
Edit:
Running with Olivier's idea of allowing commas:
#"^(\$(?!.*%))?[+-]?(\d{1,3}((,\d{3})*|\d*))?(\.\d+)?\b%?$"
Expanded to make it easier to understand:
#"^ #Require matching from the beginning of the line
(\$(?!.*%))? #Optionally allow a $ here, but only if there's no % later on.
[+-]? #Optionally allow + or - at the beginning
(
\d{1,3} #Covers the first three numerals
((,\d{3})*|\d*) #Allow numbers in 1,234,567 format, or simply a long string of numerals with no commas
)? #Allow for a decimal with no leading digits
(\.\d+)? #Optionally allow a period, but only with numerals behind it
\b #Word break (a sneaky way to require at least one numeral before this position, thus preventing an empty string)
%? #Optionally allow %
$" #End of line