Regex for US Zip code - but make it optional - angularjs

I need a regex for US zip codes. I've found lots of examples but none that allow for the zip code to also be optional. You see, I am using this on a non-required input field so the valid scenarios are a 5 digit zip code, a 5+4 digit zip code, or no zip code at all. Here is what I have so far but it does not work for the "no zip code at all" scenario:
^(\d{5}(?:\-\d{4})?)$

You need to enclose the whole pattern with a non-capturing optional group:
^(?:\d{5}(?:-\d{4})?)?$
^^^ ^^
See the regex demo
Details:
^ - start of string
(?: - start of the optional group
\d{5} - 5 digits
(?:-\d{4})? - optional group matching - and 4 digits
)? - end of the outer optional group
$ - end of string.
Note that it is the ? quantifier that makes the outer non-capturing group optional (match 1 or 0 times).
You might as well use capturing groups if you want to make your pattern shorter, but it a non-capturing one is more natural to use here since you are not interested in captured subvalues.

Related

Regex time field validation fails in ReactJS

I am following this question's solution to validate a Django's TimeField object 06:00:00
in reactjs. The validation fails with the error "Checkin Time" with value "06:00:00" fails to match the required pattern: /^([0-9]{2})\:([0-9]{2})$/ What could be wrong with this regex.
This is my Joi schema. Kindly assist
schema = {
checkin: Joi.string()
.regex(/^([0-9]{2})\:([0-9]{2})$/)
.label("Checkin Time"),
checkout: Joi.string()
.regex(/^([0-9]{2})\:([0-9]{2})$/)
.label("Checkout Time"),
};
The problem is caused by the ^ and $ characters in your regex. They denote the start and end of your string to compare. You basically are saying the value must be exactly two sets of two digit numbers, separated by a colon. However, your example includes seconds (three sets of two digit numbers, separated by colons). In order to support that, while leaving the seconds portion optional, you can do something like this:
/^([0-9]{2}):([0-9]{2})(:[0-9]{2})?$/
We basically create another capture group containing the colon and the seconds portion, and make it optional (? matches 0 or 1 instance of the pattern)

Regular expression to check multiple phone number with and without extensions

In my angular application, I've a text field to get phone number from user.
User can enter multiple phone numbers
separated by comma (Without country code and omitting 0 of city code)
For example if the phone number is 042-1234567, User will enter 421234567 and multiple numbers will be like
421234567,421234568,...
This regular expression working perfectly fine for me
^((\d{9})(,\d{9})*)$
But there's exception that user can also add extensions with phone number like this
123456786~19, 123456784~46,..
I've tried following Regx and its working fine for these kind of inputs
^((\d{9}~\d{2})(,\d{9}~\d{2})*)$
But I've to use both of these in one single field so I tried to add OR option and made Regx like this
((\d{9}~\d{2})(,\d{9}~\d{2})*)|((\d{9})(,\d{9})*)
But its not working for both kind of inputs. e.g;
Its not validating this 123456789~12,123456789~12,123456789,123456789~12
Any kind of help will be appreciated.
You may use optional groups:
^\d{9}(?:~\d{2})?(?:,\d{9}(?:~\d{2})?)*$
^^^^^^^^^^^ ^^^^^^^^^^^
See the regex demo
Details
^ - start of string
\d{9} - any 9 digits
(?:~\d{2})? - an optional non-capturing group matching
~ - a tilde
\d{2} - any 2 digits
(?:,\d{9}(?:~\d{2})?)* - zero or more sequences of:
, - a comma
\d{9}(?:~\d{2})? - see above
$ - end of string.
Note that if you need the pattern to also match an empty string, just enclose the pattern with an optional non-capturing group, ^(?:\d{9}(?:~\d{2})?(?:,\d{9}(?:~\d{2})?)*)?$.

Regex to validate particular url

I am using angularjs. I want to use validation for my url field. I am a beginner in regex expressions. I want that the url should starting with 'https' or 'http' and followed by a string(this string can be a string or ip). For E.g https://localhost or http://100.100.100.100 should be valid and ftp://localhost should be invalid as it is starting with ftp.
I am using ng-pattern to validate this field. What regex expression should i use? Appreciate your help.
The following regexp should do it, or at least be a good start:
https?:\/\/[0-9A-z.]+
What it does:
http matches the characters http literally (case sensitive)
s? matches the character s literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible
: matches the character : literally (case sensitive)
/ matches the character / literally (case sensitive)
/ matches the character / literally (case sensitive)
[0-9A-z.]+ Match any character present in the lists (0-9 = all numbers from 0 to 9, A-z = all letters case insensitive, . = matches also the . character)
+ Quantifier — Matches between one and unlimited times, as many times as possible
By the way this is simple enought and you could have figured it out by yourself. Googling url regex gives tons of other possible solutions.
If you are interested in trying out your regexs, this website will be really useful to you: regex101

Angular nothing to repeat - regex

I want to achieve something like this;
0.00
1 digit before point
2 digit after point
And I'm using the regex - /^([0-9]{1})?+(\.[0-9]{1,2})?$/ with angular but getting the below error.
Invalid regular expression: /^([0-9]{1})?+(\.[0-9]{1,2})?$/: Nothing to repeat at new RegExp (native)
Any help appreciated
You can use this regex:
/^[0-9](?:\.[0-9]{1,2})?$/
Problem is presence of ? and + quantifier after that in your regex.
RegEx Demo
?+ is invalid - the ? is a quantifier to match 0 or 1 occurrences of the previous token, and + matches one or more occurrences - you can't combine the two. (In some regex flavors, a + after a quantifier creates a possessive quantifier, but not in JavaScript).
Use
/^([0-9])?(\.[0-9]{1,2})?$/
if you plan on having both parts of the regex optional (matching 1 or 1.1 or .1 or even the empty string).

What is the best way to validate and parse complex fields in Pentaho kettle?

What is the best way to validate fields in a row and if invalid, correct it to the right form?
The simplest example would be checking phone number field (can come in variant formats -> 111-111-1111, (111) 111-1111 etc), and we would ideally want to validate these and standardize to one form (lets say: 1111111111). One way to do this is to use filter rows and then use a regex, or we can use data validator. But this will only tell us what data is invalid but not actually format it for us. We can then use Javascript modified value step to write a js script to do this. But I am guessing there is a better way (or a built in integration that I haven't come across) that would do these basic validations. Or is it recommended to just dump rows containing invalid fields in a separate csv file and then use a script to parse it separately?
g'day
i use the excellent 'replace in string' step to handle this circumstance
you can cumulatively apply rules for removing bad char from strings within the single step - it is really easy to use for single-char fixes like what you have described, and best of all, it also allows you to search based on regex as well - in a single step you have documented your standardisation and produced the clean output
in your case, i would create two 'rules' to replace ( and ) with nothing - however, the - is a little trickier; you need a rule for each removal of a single char, so you would need to know the maximum number of - in a single data field, then add this many lines to your 'replace in string' step
if this is unpalatable, consider the 'user defined java expression' and a call to replace, eg: ( (t0 != null) ? t0.replace("-","") : t0 )
as i stated, each 'fix' is applied in sequential order - the In stream field is the input field-name, whereas the Outstream field is left blank instructing d.i. to modify the field itself - here's a more complex example where i search for regex and replace them with nothing, escape for the case where i escape a " double-quote:
In stream field Out stream field use RegEx Search Replace with
sc_srcuri N {Internal.Transformation.Filename.Directory}
re_s_sciname Y ["] \\"
re_s_sciname Y .[\x08]
re_s_sciname Y .[\x08]
re_s_sciname Y .[\x08]
re_s_sciname Y [*]
re_s_sciname Y \s*$
re_s_sciname Y ^\s*
notice i am removing up to three 'delete' control-codes [\x08] from this particular string?

Resources