I have a regular expression for phone numbers as follows:
^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
I have a mask on the phone number textbox in the following format: (___)___-____
How can I modify the regular expression so that it accommodates the mask?
The expression for the (placeholder) mask is
^\(_{3}\)_{3}-_{4}$
The expression for a valid phone number is
^\(\d{3}\)\d{3}-\d{4}$
The mask uses _ in place of digits, so you'll need to use [\d_] as your character class to match as the user is typing.
^\([\d_]{3}\)[\d_]{3}-[\d_]{4}$
Obviously, if the user switches fields, you'll want to return an error if your phone field as any remaining _ in it. phone.match(/_/) == null should do the trick here :)
Your question is a little unclear; if you want a regular expression that matches that mask, it's:
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
ValidationExpression="\([2-9]\d{2}\)\d{3}-\d{4}$|^\(_{3}\)\ _{3}-_{4}$"
This will validate the mask and (234)432-4322 but won't allow the user to enter (434)88_-____
this expression will work for you
^[( ][0-9][1-9][0-9][) ]?[0-9]{3}[- ]?[0-9]{4}$
this expression defines that except second digit of phone number every digit will consider
{0}
Related
I want to validate the user input,
it should accept,
1+2
1.2+56+3.5
it should not accept any alphabets, special characters other than . and +
and mainly it should not accept ++ and ..
please help me with regular expression.
This should work:
var regex = /^[0-9]+(\.[0-9]+)?(\+[0-9]+(\.[0-9]+)?)*$/;
"1+2".match(regex); // not null
"1.2+56+3.5".match(regex); // not null
"1++2".match(regex); // null
"1..2".match(regex); // null
online: http://regex101.com/r/zJ6tP7/1
Something like this should suffice
^([+]?\d+(\.\d+)?)*$
http://regex101.com/r/qE2kW1/2
Source: Validate mathematical expressions using regular expression?
Note that I'm assuming it should not accept .+ or +. as well. I'm not assuming that you require checking for multiple decimals prior to an addition, meaning this will accept 3.4.5 I'm also assuming you want it to start and end with numbers, so .5 and 4+ will fail.
(\d*[\.\+])*(\d)*
This takes any amount of number values, followed by a . or a +, any number of times, followed by any other number value.
To avoid things like 3.4.5 you'll likely need to use some sort of lookaround.
EDIT: Forgot to format regular expression.
I need to remove the space between the numbers using RegEx means I want to make a formated number from the input like {lhs: \"1000 U.S. dollars\",rhs: \"58 740.6015 Indian rupees\",error: \"\",icc: true} I tried the following expression Regex regex = new Regex("rhs: \\\"(\\d+.\\d*)"); but its give me a error "Input format not correct" so how can I remove the space between the numbers
P.S The currency name will change each time it called
use the following expression
string value = Regex.Replace(response.Split(',')[1], "[^.0-9]", "");
I have tried creating a regular expression myself to do this, but honestly my mind is so boggled with it right now that I must ask for help... This may be helpful for people in the future as well.
I have the following input templates:
06-6A-BF-05-AF-84-DF-A4-23-7C-BE-B4-6C-95-D7
JK1T-XTSRV-2HC4D-RP4S7-ZMKRG
I need to pick out strings like these two from an input string. An input string may look like this:
JK1T-XTSRV-2HC4D-RP4S7-ZMKRG
FDGF-A1S0M-5M8XJ-T08WC-BCZSJ
C6-6C-1C-17-B7-EE-BE-EA-E3-7C-EF-23-6C-12-F1
asdf234 ,f C6-324_EE
In this case, the following would be returned:
JK1T-XTSRV-2HC4D-RP4S7-ZMKRG, FDGF-A1S0M-5M8XJ-T08WC-BCZSJ, C6-6C-1C-17-B7-EE-BE-EA-E3-7C-EF-23-6C-12-F1
Thus, the regular expression would need to have the following restrictions to match a string:
15 two character (numbers or letters) pairs separated by -
5 four character (numbers or letters) pairs separated by -
What regular expression will match these?
You should use two regular expressions:
(\w{2}-){14}\w{2}
\w{4}-(\w{5}-){3}\w{5}
The second type is actually one four char and four five char.
Test 1:
http://fiddle.re/h3ve6
Test 2:
http://fiddle.re/3a5e6
I tried many syntax in vistal studio, and in this site, but nothing helped.
The expression would be _ct_(anyDigitHere)_
like
adkasjflasdjfsdf asdfkl sjfdsf _ct150_ asdfasd // so it would match this _ct150
any thing here doens't matter Random stuff..afd a&r9qwr89 ((
_ct415487_ anything here doesn't matter // this will match _ct415487_
basically any _ctAndAnyNumberHere_ (underscore at start and end)
A couple I tried ^*(_ct)(:z)(+_)+*$, ^*(_ct[0-9]+_)+*$. But none helps!
EDIT
Thanks for the reply(ies). That did work, but what I now the problem is replace those matched elements with a hidden field value.. say..
if the value in hidden field is of 1 digit, (any value from 0-9), I have to take last digit from that matched expression and replace it with the value in hidden field.
if the value in hidden field is of 2 digit, (any value from 0-99), I have to take last two digits from that matched expression and replace it with the value in hidden field.
so, basically..
if the value in hidden field is of n digit, I have to take last n digits from that matched expression and replace it with the value in hidden field.
How do I do that?
I don't know what language of visual studio you're talking about, but this should work:
_ct\d+_
or this:
_ct([0-9]+)_
EDIT:
Regex rg = new Regex("_ct([0-9]+)_");
string text = "any thing here doens't matter Random stuff..afd a&r9qwr89 ((_ct415487_ anything here doesn't matter";
var match = rg.Match(text).Groups[1].Value;
int sizeHiddenField = HiddenField1.Value.Length;
var newString = text.Replace(match, match.Substring(0, match.Length - sizeHiddenField) + HiddenField1.Value);
/_ct\d*_/
This is the regular expression syntax for your given problem. Try this
How do you create a regular expression for a certain string? And can you do it in the Assertion (precondition part of the code)?
I've been google-ing around but couldn't get anything convincing.
The question is like this:
Add a precondition to the DEPARTMENT (the class that we're working on) creation procedure that ensures that the phone number is valid. There are three possible valid phone number formats. A valid phone number consists of one of:
eight digits, the first of which is non-zero
a leading zero, a single non-zero digit area code, and then eight digits, the first of
which is non-zero
a leading ‘+’, followed by a two digit country code, then a single non-zero digit
area code, and then eight digits, the first of which is non-zero
Any embedded spaces are to be ignored when validating a phone number.
It is acceptable, but not required, to add a PHONE_NUMBER class to the system as part of
solving this problem.
There are several different questions to be answered:
How to check if a given string matches a specified regular expression in Eiffel? One can use a class RX_PCRE_MATCHER from the Gobo library. The feature compile allows setting the required regular expression and the feature recognizes allows testing if the string matches it.
How to write a regular expression for the given phone number specification? Something like "(|0[1-9]|\+[0-9]{2}[1-9])[1-9][0-8]{7}" should do though I have not checked it. It's possible to take intermediate white spaces into account in the regular expression itself, but it's much easier to get rid of them before passing to the regular expression matcher by applying prune_all (' ') on the input string.
How to add a precondition to a creation procedure to verify that the argument satisfies it? Let's assume that from the previous items we constructed a function is_phone_number that takes a STRING and returns a BOOLEAN that indicates if the specified string represents a valid phone number. A straightforward solution would be to write
make (tel: STRING)
require
is_phone_number (tel)
...
and have a feature is_phone_number in the class DEPARTMENT itself. But this prevents us from checking if the specified string represents a phone number before calling this creation procedure. So it makes sense to move is_phone_number to the class PHONE_NUMBER_VALIDATOR that class DEPARTMENT will inherit. Similarly, if PHONE_NUMBER needs to validate the string against specified rules, it can inherit PHONE_NUMBER_VALIDATOR and reuse the feature is_phone_number.
Halikal actually worked this one out, but dudn't share until now ...
This works in eiffelStudio 6.2 (note - this is gobo)
http://se.inf.ethz.ch/old/people/leitner/gobo_guidelines/naming_conventions.html
A valid phone number consists of one of:
eight digits, the first of which is non-zero
a leading zero, a single non-zero digit area code,
and then eight digits, the first of which is non-zero
a leading + followed by a two digit country code,
then a single non-zero digit area code, and then eight digits,
the first of which is non-zero
Any embedded spaces are to be ignored when validating a phone number.
require -- 040 is ascii hex space
valid_phone:
match(phone, "^\040*[1-9]\040*([0-9]\040*){7}$") = TRUE or
match(phone, "^\040*0\040*([1-9]\040*){2}([0-9]\040*){7}$") = TRUE or
match(phone, "^\040*\+\040*([0-9]\040*){2}([1-9]\040*){2}([0-9]\040*){7}$") = TRUE
feature --Regular Expression check
match(text: STRING; pattern: STRING): BOOLEAN is
-- checks whether 'text' matches a regular expression 'pattern'
require
text /= Void
pattern /= Void
local
dfa: LX_DFA_REGULAR_EXPRESSION --There's the Trick!
do
create dfa.make
dfa.compile(pattern, True) --There's the Trick!
check -- regex must be compiled before we can use it
dfa.is_compiled;
end
Result := dfa.matches(text)
-- debug: make sure of which pattern
if dfa.matches (text) then
io.putstring(text + " matches " + pattern + "%N")
end
end
end