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
Related
I have the following Regular Expression which matches an email address format:
^[\w\.\-]+#([\w\-]+\.)+[a-zA-Z]+$
This is used for validation with a form using JavaScript. However, this is an optional field. Therefore how can I change this regex to match an email address format, or an empty string?
From my limited regex knowledge, I think \b matches an empty string, and | means "Or", so I tried to do the following, but it didn't work:
^[\w\.\-]+#([\w\-]+\.)+[a-zA-Z]+$|\b
To match pattern or an empty string, use
^$|pattern
Explanation
^ and $ are the beginning and end of the string anchors respectively.
| is used to denote alternates, e.g. this|that.
References
regular-expressions.info/Anchors and Alternation
On \b
\b in most flavor is a "word boundary" anchor. It is a zero-width match, i.e. an empty string, but it only matches those strings at very specific places, namely at the boundaries of a word.
That is, \b is located:
Between consecutive \w and \W (either order):
i.e. between a word character and a non-word character
Between ^ and \w
i.e. at the beginning of the string if it starts with \w
Between \w and $
i.e. at the end of the string if it ends with \w
References
regular-expressions.info/Word Boundaries
On using regex to match e-mail addresses
This is not trivial depending on specification.
Related questions
What is the best regular expression for validating email addresses?
Regexp recognition of email address hard?
How far should one take e-mail address validation?
An alternative would be to place your regexp in non-capturing parentheses. Then make that expression optional using the ? qualifier, which will look for 0 (i.e. empty string) or 1 instances of the non-captured group.
For example:
/(?: some regexp )?/
In your case the regular expression would look something like this:
/^(?:[\w\.\-]+#([\w\-]+\.)+[a-zA-Z]+)?$/
No | "or" operator necessary!
Here is the Mozilla documentation for JavaScript Regular Expression syntax.
I'm not sure why you'd want to validate an optional email address, but I'd suggest you use
^$|^[^#\s]+#[^#\s]+$
meaning
^$ empty string
| or
^ beginning of string
[^#\s]+ any character but # or whitespace
#
[^#\s]+
$ end of string
You won't stop fake emails anyway, and this way you won't stop valid addresses.
\b matches a word boundary. I think you can use ^$ for empty string.
^$ did not work for me if there were multiple patterns in regex.
Another solution:
/(pattern1)(pattern2)?/g
"pattern2" is optional. If empty, not matched.
? matches (pattern2) between zero and one times.
Tested here ("m" is there for multi-line example purposes): https://regex101.com/r/mezfvx/1
I'm really new into regex and currently getting some trouble to solve a problem. I will appreciate any help :)
Using ruby 2.4.2
The problem: Split a string at every dot, except when the asd word is after the dot
String: str = "qwer.qwer.asd"
Code: str.split(/\./)
Output: ["qwer", "qwer", "asd"]
The output should be: ["qwer", "qwer.asd"]
Use
str.split(/\.(?!asd\b)/)
The \.(?!asd\b) pattern matches any dot that is not followed with asd followed with a word boundary. The (?!asd\b) is a negative lookahead that fails the match if the lookahead pattern finds a match immediately to the right of the current location.
In case the "word" ends with a period or end of string, use
str.split(/\.(?!asd(?:\.|\z))/)
where (?:\.|\z) is a non-capturing group matching either a dot (\.) or (|) end of string (\z).
See the Ruby demo and a regex demo.
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).
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/
I'm trying to build a .NET regex to match SQL Server constant strings... but not Unicode strings.
Here's a bit of SQL:
select * from SomeTable where SomeKey = 'abc''def' and AnotherField = n'another''value'
Note that within a string two single quotes escapes a single quote.
The regex should match 'abc''def' but not n'another''value'.
I have a regex now that manages to locate a string, but it also matches the Unicode string (starting just after the N):
'('{2})*([^']*)('{2})*([^']*)('{2})*'
Thanks!
This pattern will do most of what you are looking to do:
(?<unicode>n)?'(?<value>(?:''|[^'])*)'
The upside is that it should accurately match any number of escaped quotes. (SomeKey = 'abc''''def''' will match abc''''def''.)
The downside is it also matches Unicode strings, although it captures the leading n to identify it as a Unicode string. When you process the regular expression, you can ignore matches where the match group "unicode" was successful.
The pattern creates the following groups for each match:
unicode: Success if the string is a Unicode string, fails to match if ASCII
value: the string value. escaped single quotes remain escaped
If you are using .NET regular expressions, you could add (?(unicode)(?<-value>)) to the end of the pattern to suppress matching the value, although the pattern as a whole would still match.
Edit
Having thought about it some more, the following pattern should do exactly what you wanted; it will not match Unicode strings at all. The above approach might still be more readable, however.
(?:n'(?:''|[^'])*'[^']*)*(?<!n)'(?<value>(?:''|[^'])*)'