UK postcode regex validation - regex

I was looking for a valid UK postcode regex pattern, but seems that any of them work as a traditional or normal regex.
I test all patterns using this tool: https://regex101.com/
The regex I found in this document https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/413338/Bulk_Data_Transfer_-_additional_validation_valid_from_March_2015.pdf is not working.
I tried all regex in this wiki page https://en.wikipedia.org/wiki/Talk:Postcodes_in_the_United_Kingdom but nothing.
Am I doing something wrong?
I'm writing an Angular directive.
var regex = ????;
// add a parser that will process each time the value is
// parsed into the model when the user updates it.
ngModel.$parsers.unshift(function(value) {
// test and set the validity after update.
var valid = regex.test(value);
console.log(valid);
ngModel.$setValidity('ukPostcode', valid);
// if it's valid, return the value to the model,
// otherwise return undefined.
return valid ? value : undefined;
});
Thank you.

When your regex is corrected it matches both:
rg224ph
rg141de
Your regex currently has two errors:
[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})
^ {1,2} Preceding token is not quantifiable
and
[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})
^ ) Unbalanced parenthesis
I presume that the regex you intended was:
([A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})
Which matches both inputs.

Related

how to implement regex in Angular ng-select?

I have already implemented angular multi-select Dropdown. Now I want it to search using RegEx. Like if I have qwertyuiop, and if I wrote w*i than it should suggest me all the entries who contains 'W' and 'I' in the same string.
Do you want to know the whole thing, like how to bind the input of an input field, read the input, create an RegEx out of it, and then use the RegEx as a filter to the list you are showing in your dropdown ?
Or only the RegEx part?
For the part with RegEx:
You should take a look at the Javascript Defintion of RegEx (for example at Mozilla Developer Network ). It has a quite nice functionality.
let input:string = // the value the user typed, like w*i
const regEx = new RegExp(input)
let myDropdownList:string[] = // the list of strings i want to filter
let filteredDropdownList = myDropdownList.forEach((value:string)=>{
return regEx.test(value)
})
What happens here?
You are creating a regular Expressen with new RegExp(someString).
You can optimize it with RegEx flags.
Later you test a string with myRegEx.test(theString). It will return true if the regEx founds at least one match in the string.
I hope this helps you a bit.
warm regards.

regular expression to find a repeated code

I am trying to write a reg expression to find match of strings / code in a database.
here is some of the sample code / string which i need to remove using the regular expression.
[b:1wkvatkt]
[/b:1wkvatkt]
[b:3qo0q63v]
[/b:3qo0q63v]
[b:2r2hso9d]
[/b:2r2hso9d]
Anything that match [b:********] and [/b:********]
Anybody please help me out. Thanks in advance.
You can use the following pattern (as stated by LukStorms in the comments):
\[\/?b:[a-z0-9]+\]
If you want to replace [b:********] with <b> (and also the closing one), you can use the following snippet (here in JavaScript, other languages are similar):
var regex = /\[(\/)?b:[a-z0-9]+\]/g;
var testText = "There was once a guy called [b:12a345]Peter[/b:12a345]. He was very old.";
var result = testText.replace(regex, "<$1b>");
console.log(result);
It matches an optional / and puts it into the first group ($1). This group can then be used in the replacement string. If the slash is not found, it won't be added, but if it is found, it will be added to <b>.

Regular Expression Validation In ActionScript

I need to validate a regular expression aganist a string veryt strictly only what is there in string. But at present i don't see such a function for regular expression since bot exec and test doesn't check strictly. for example say i have regular expression for validating 43a^2b^2, but even its 43a it's returning true, as a part of string is present in reg exp. i need true only when exact match is found, is there any way achieve this?
The code I am using at present:
var isRegExp:Boolean = regExp.test(value1);
Thanks in advance...
Using the beginning of string ^ and end of string $ tests should work.
var regExp:RegExp = /^43a$/;
regExp.test('43a'); //true
regExp.test('43a^2b^2'); //false
You can read more about anchors here.

Regular expression for length validation in email address

We have a textbox which accepts comma separated email address. The regular expression is
^(\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,]?\b)*$.
However now I am unable to add a length validation to this expression. Each email address should not be more than 150 characters. I have tried
^((\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)){1,150}\s*[,]?\b)*$.
But it doesn't work.
Can you please modify the regex so that it also implements this validation too.
Why use a regexp when you can simply check the length of the string? If you must, use a second regexp:
^.{1,150}$
Use a lookahead
/(?=^.{1,150}$)(your regex goes here)/
I would rather not complicate this regex further and add explicit length check before checking that e-mail matches. In JavaScript it will be something like the following:
function validateEmail(email) {
var regex = /^(\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,]?\b)*$/;
// 20 is used instead of 150 for sake of example
return email.length <= 20 && regex.test(email);
}
// validateEmail("jdoe#example.com") == true
// validateEmail("loooooooooooooooooooonjohn#example.com") == false
By the way, dot after $ in your regex seems to be a mistake, so I skipped it in my snippet.
^([a-zA-Z][\w\.-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){0,70}$
I arrived here looking for a way to extend my ng-pattern for e-mail for an Angular form.
Taking Rorick's approach of checking for length separately, I found a much easier solution: just set ng-maxlength="150" in the input. Then you can customize an error message that tells the user that the e-mail is too long.
maxlength="150" works fine too, preventing any extra characters from being added to the field, but I liked that ng-maxlength tells you what's wrong rather than truncating your string.

MFC: How do I construct a good regular expression that validates URLs?

Here's the regular expression I use, and I parse it using CAtlRegExp of MFC :
(((h|H?)(t|T?)(t|T?)(p|P?)(s|S?))://)?([a-zA-Z0-9]+[\.]+[a-zA-Z0-9]+[\.]+[a-zA-Z0-9])
It works fine except with one flaw. When URL is preceded by characters, it still accepts it as a URL.
ex inputs:
this is a link www.google.com (where I can just tokenize the spaces and validate each word)
is...www.google.com (this string still matches the RegEx above :( )
Please help...
Thanks...
Use the IgnoreCase flag instead of catering for each case.
Stick a ^ at the beginning if you want the start of the string to be the start of the URL
You're missing a lot of characters from possible, valid URLs.
You need to tell the regex to only match at the start and end of the string. I'm not sure how you do that in VC++ - in most regexs you enclose the pattern with ^ and $. The ^ says "the start of the string" and the $ says "the end of the string."
^(((h|H?)(t|T?)(t|T?)(p|P?)(s|S?))\://)?([a-zA-Z0-9]+[\\.]+[a-zA-Z0-9]+[\\.]+[a-zA-Z0-9])$
The second is matching because the string still contains a valid URL.
How about using CUrl (that is, 'C-Url', in ATL, not curl as in libcurl) which can 'parse' urls with CUrl::CrackUrl . If that function returns FALSE you assume it's not a valid URL.
That said, decomposing URL is sufficiently complex to warrant a proper parser, not a regex based decomposition. Cfr. rfc 2396 etc. for an overview on the complexities.
Start the regex with ^ to and end it with $ to have the regex match only if the entire sting matches (if that's what you want):
^(((h|H?)(t|T?)(t|T?)(p|P?)(s|S?))\://)?([a-zA-Z0-9]+[\.]+[a-zA-Z0-9]+[\.]+[a-zA-Z0-9])$
What about this one: (((f|ht)tp://)[-a-zA-Z0-9#:%_\+.~#?&//=]+) ?
This Regular Expression has been tested to work for the following
http|https://host[:port]/[?][parameter=value]*
public static final String URL_PATTERN = "(https?|ftp)://(www\\.)?(((([a-zA-Z0-9.-]+\\.){1,}[a-zA-Z]{2,4}|localhost))|((\\d{1,3}\\.){3}(\\d{1,3})))(:(\\d+))?(/([a-zA-Z0-9-._~!$&'()*+,;=:#/]|%[0-9A-F]{2})*)?(\\?([a-zA-Z0-9-._~!$&'()*+,;=:/?#]|%[0-9A-F]{2})*)?(#([a-zA-Z0-9._-]|%[0-9A-F]{2})*)?";
PS. It also validates on localhost link.
(Thoroughly written by me :-))