Angular nothing to repeat - regex - angularjs

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).

Related

Regex: Match string pattern with AND condition

Is there a regex which can match the strings 1 and 2 but not 3:
TABLE_SP_02.csv.gz --match
TABLE.csv.gz --match
TABLE_REMARK.csv.gz --not match
I have many files with TABLE_SP format so I would like to match String 2 and all other string starting with TABLE_SP
Any help is appreciated. Thanks!
I tried a few regex patterns. I was able to individually match String 1 and 2. I could write a regex to match them both together. I am using regex_substr function in Snowflake for the same. I tested out the pattern in regex 101:
enter image description here
This matches the first 2 but not the 3rd.
^TABLE(_SP|\.)(.*)?csv.gz$
https://regex101.com/r/UE9I5C/1
TABLE(_SP|.) this regex pattern is solving the problem.

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

Regex for US Zip code - but make it optional

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.

Matching Regular Expressions In SQL Server

I am trying to extract id of Android app from its url but getting extra characters.
Using replace function in sql server, below are two sample urls:
https://play.google.com/store/apps/details?id=com.flipkart.android&hl=en com.flipkart.android
https://play.google.com/store/apps/details?hl=en_US&id=com.surveysampling.mobile.quickthoughts&referrer=mat_click_id%3Df1901cef59f79b1542d05a1fdfa67202-20150429-5128 en_US&id=com.surveysampling.mobile.quickthoughts&r
I am doing this right now:
SELECT
SUBSTRING(REPLACE(PREVIEW, '&hl=en',''), CHARINDEX('?', PREVIEW) + 4 , 50)
FROM OFFERS_TABLE;
But for 1st I am getting com.flipkart.android which is correct, but for 2nd I am getting en_US&id=com.surveysampling.mobile.quickthoughts&r.
I want to remove en_US&id from starting of it and &r from its end.
Can someone help me with any post or url from where I can refer?
What you are actually trying to do is extract the string preceded by id= until the & is found which is separator for variables in URL. Taking this condition I came up with following regex.
Regex: (?<=id=)[^&]*
Explanation: It uses the lookbehind assertion that is the string is preceded by id= until the first & is found.
Regex101 Demo
It seems like you've made some assumptions of lengths. The the &r is appearing because that is 50 characters. You are also getting the en_US because you assumed 4 characters at the beginning but your second string has more. Perhaps you can split on & and then look for the variable that begins with id=.
it seems like a function like this would help.
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/

Resources