I'm looking for a regular expression that accepts only numerical values and no spaces.
I'm currently using:
^(0|[1-9][0-9]*)$
which works fine, but it accepts values that consist ONLY of spaces. What is wrong with it?
The reason why is that * will accept 0 or more. A purely empty string has 0 numbers and hence meets the requirements. You need 1 or more so use + instead.
^(0|[1-9][0-9]+)$
EDIT
Here is Andrews more robust and simpler solution.
^\d+$
this regex works perfecttly
^\d*[0-9](|.\d*[0-9])?$
Related
i have to write the Regular expression that accept two signs only + and - and can be of any no. of digit but + should be the first sign if it is there
I am assuming the following:
+ may only be at the beginning of the number
- should not be
at the beginning or end of the number
consecutively repeated
You could try this regular expression:
^\+?(?:[\d]+\-)*[\d]+$
Passes the following string examples:
+12-34-5678-90
12-345-678-90
1234567890
Fails the following string examples:
++12-34-5678-90
12+34-5678-90
12-34-5678-90-
12--34-5678-90
EDIT
As #Alovchin pointed out, the OP expressed the need to allow for dots (.) as well in the numbers in one of the answer comments. Although this requirement does not reflect in the question I am going to add it here just-in-case.
If the requirement is to allow for dots (.) as well then the above regex will need to be updated to the following.
^\+?(?:[\d]+[\-\.])*[\d]+$
Hope this helps.
Try this one
^\+?[0-9-]+$
Check here This is for indian numbers.
In C# try this one:
^\+?(?![\-\.])[\d\-\.]+(?<![\-\.])$
There's a better answer by Tanzeel Kazi.
How do I restrict single zero in a numeric (Decimal may alow) textbox? Textbox can accept any number but it should not accept only zero or "0.##" as value.
For example: "900.55", "200.00" is valid but "0" and "0.105"is invalid.
I tried ^[1-9]\d\.?\d[0-9]* but it accepting the "0" and "0.##"
You're almost there.
^[1-9][0-9]*(\.[0-9]+)?$
The input must
start with a number 1-9
be followed by any sequence of 0-9
and optional:
a dot followed by one or more 0-9.
Notes:
This also disallows 3.. If you don't want that, replace the last + with a *.
You can use \d instead of [0-9]. I've used the latter to stay consistent with [1-9] and keep things simple.
I think you're close, but it may be easier to include the decimal and following digits in a group and make that entire group only allowable once like this one:
^[1-9][0-9]*(\.\d*)?
Also, here's a useful site for testing regular expressions.
I think it would be simpler to do this:
float result;
return float.TryParse(textBox1.Text, out result) && result < 1;
If the textbox only accepts valid numbers, and all you want to assert is that it is > 0. All you really need then is
^[1-9]
or if trailing prefix zero's are allowed
^0*[1-9]
You could alternatively write it using a negative lookahead:
^(?!0\b)\d+(\.\d*)?$
This has the added bonus of accepting numbers with a preceding 0 like 022.
I faced the same situation and solve this.
try out it.
#"[^0]*[0-9].\d{2}$
I am trying to use a regex to get the value after a character. In this case, the string is m325 and I need to get whatever is after the m.
Can anybody tell me what is wrong with my code?
Regex rgMeter = new Regex("m(.+$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID));
Update:
Thanks for all your answers...for some reason the regex "m(.+)$" is returning the m as well as the string I require. I have tried the Groups example and it returns the data that I want. Why do I need to use Groups to do this?
Apart from the missing ), you oversimplified it a bit. You need to do
Regex rgMeter = new Regex("m(.+)$");
intMeterID = Convert.ToInt32(rgMeter.Match(strID).Groups[1].Value);
(Possibly, you might want to add a check if the Match() matched or not.)
You are missing a closing ). Plus, if you are extracting a number you should limit yourself to digits only, Will avoid trying to parse a faulty string into integer in the next statement.
m(\d*)
"m(.+)$" - there wasn't closed (
Also, you can test it on: http://gskinner.com/RegExr/
What values can appear behind the "m" character?
If it's only an integer, the I should use the solution Shekhar_Pro provided..
If any character, go with the rest :)
The regex you require is /^m(.*)$/
Actually you should use \d or [0-9] if you want match digits.
/^m([0-9]*)$/
and
/^m([0-9]{3})$/
if there are always 3 digits
Looks like you missed the closing )
A simple "m\d*" should do this.. please show us whole string to see the case.
Basically, the input field is just a string. People input their phone number in various formats. I need a regular expression to find and convert those numbers into links.
Input examples:
(201) 555-1212
(201)555-1212
201-555-1212
555-1212
Here's what I want:
(201) 555-1212 - Notice the space is gone
(201)555-1212
201-555-1212
555-1212
I know it should be more robust than just removing spaces, but it is for an internal web site that my employees will be accessing from their iPhone. So, I'm willing to "just get it working."
Here's what I have so far in C# (which should show you how little I know about regular expressions):
strchk = Regex.Replace(strchk, #"\b([\d{3}\-\d{4}|\d{3}\-\d{3}\-\d{4}|\(\d{3}\)\d{3}\-\d{4}])\b", "<a href='tel:$&'>$&</a>", RegexOptions.IgnoreCase);
Can anyone help me by fixing this or suggesting a better way to do this?
EDIT:
Thanks everyone. Here's what I've got so far:
strchk = Regex.Replace(strchk, #"\b(\d{3}[-\.\s]\d{3}[-\.\s]\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]\d{4}|\d{3}[-\.\s]\d{4})\b", "<a href='tel:$1'>$1</a>", RegexOptions.IgnoreCase);
It is picking up just about everything EXCEPT those with (nnn) area codes, with or without spaces between it and the 7 digit number. It does pick up the 7 digit number and link it that way. However, if the area code is specified it doesn't get matched. Any idea what I'm doing wrong?
Second Edit:
Got it working now. All I did was remove the \b from the start of the string.
Remove the [] and add \s* (zero or more whitespace characters) around each \-.
Also, you don't need to escape the -. (You can take out the \ from \-)
Explanation: [abcA-Z] is a character group, which matches a, b, c, or any character between A and Z.
It's not what you're trying to do.
Edits
In response to your updated regex:
Change [-\.\s] to [-\.\s]+ to match one or more of any of those characters (eg, a - with spaces around it)
The problem is that \b doesn't match the boundary between a space and a (.
Afaik, no phone enters the other characters, so why not replace [^0-9] with '' ?
Here's a regex I wrote for finding phone numbers:
(\+?\d[-\.\s]?)?(\(\d{3}\)\s?|\d{3}[-\.\s]?)\d{3}[-\.\s]?\d{4}
It's pretty flexible... allows a variety of formats.
Then, instead of killing yourself trying to replace it w/out spaces using a bunch of back references, instead pass the match to a function and just strip the spaces as you wanted.
C#/.net should have a method that allows a function as the replace argument...
Edit: They call it a `MatchEvaluator. That example uses a delegate, but I'm pretty sure you could use the slightly less verbose
(m) => m.Value.Replace(' ', '')
or something. working from memory here.
I am not too good with regular expressions so this might be an obvious question.
I want my expression to match if a certain number of characters are found and fail if any extra characters are present. For example if I have a string that should have 4 digits the following should be true.
1234 - match
ab1234cd - does not match
012345 - does not match
What I have so far is \d{4} but my understanding is that this would just match any string that has 4 digits together in it anywhere. I want to match only if a string contains 4 digits and nothing else.
Any help would be appreciated. Thanks.
Use ^ and $ to mark the start/end of the string.
Depending on how you are implementing it (single line mode or multiline mode) you can use something similar to:
^\d{4}$
To only match the (beginning of the string) four digits (end of string).
\b[0-9]{4}\b or ^\d{4}$ should both work. Maybe I could expand a little bit on what GrayWizardx said (just in case you do not use Regular Expressions in C# that much), the regular expressions provided above look for lines that have only 4 digits. By default (if memory serves me well), the regular expression engine looks at the first line only, so if you have a string made from more than 1 line and you would like to check the entire string (for instance, the string has been loaded from a file), you would add the option RegexOptions.MultiLine. in this way, the engine will take a look at the other lines as well.
Hope this has been helpful :)
I believe \b[0-9]{4}\b should do the trick.