Regular Expression Validation In ActionScript - regex

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.

Related

Regular expression test ; (\bSubject:\b)(?!=\bSubject:test.com\b)

Ok I need this to be working.
As postfix bodycheck go through each line...I need some sort of if else.
If this is subject line and I want to check subject and NOT subject:test.com to be true
(\bSubject:\b)(?!=\bSubject:test.com\b)
This is not working.
Sample line:
Subject:test.com - testing email
If the lookahead is supported, you could write the pattern as:
\bSubject:(?!test\.com\b).*
Regex demo

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

UK postcode regex validation

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.

Regular expression that contains one expression yet doesn't contain the other

We are currently matching "service_hub*queue"
I want to ignore the case "service_hub_scout_dead_queue" and yet still match everything else.
What is the regular expression for that ?
This javascript sollution gives an array with the matches
var myText = 'service_hub_anything_queue Add service_hub_scout_dead_queue something service_hub_someting_queue else';
var myMatches = myText.match(/service_hub(?!_scout_dead_)\w+queue/g);
If you are rather interested in what follows a match
var mySplit = ('dummy'+myText).split(/service_hub(?!_scout_dead_)\w+queue/g).filter(function(txt,i) {return (i>0);})
I put 'dummy' and then filter away the first part to make it work both if the sting starts with a valid tag and when it does not.
Using negative lookbehind: "service_hub_.*?(?<!_scout_dead)_queue"
This appears to be widely supported by popular regex engines; I've tested with Java (or Scala, rather) just to make sure it works.

Regular Expression only match if String ends with target

I need a regular expression that will only match to the String if it ends with the target that I am looking for. I need to locate a file with a specific extension, problem is this extension also comes in other files. For example I have two files named
B82177_2014-07-08T141507758Z.ccf
and
B82177_2014-07-08T141507758Z.ccf.done
I only want to grab the first of these and my pattern is:
.*\.ccf
but this grabs both.
Any suggestions appreciated, I am a newbie to regular expressions.
Use an end anchor ($):
.*\.ccf$
This will match any string that ends with .ccf, or in multi-line mode, any line that ends with .ccf.
$ is used to match the end of the string.
and can be used like
"string"$
like
xyz$
if you want to end with xzy
VanilaJS function base example
function isValidUrl(x){
let expression = /[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)?/gi;
let regex = new RegExp(expression);
return x.match(regex) ? true : false
}
console.log( isValidUrl('www.domain.com') ) // true
console.log( isValidUrl('nothing') ) // false