Using Regular Expression on Live Validation - regex

I'm using LiveValidation (http://livevalidation.com/) to validate a form on my site in the client-side part.
However i'm having problems using the regular expressions:
the example on the website is not very clear and i'm trying to validate a field where i don't want to reject numbers with this:
var f1a = new LiveValidation('nome');
f1a.add( Validate.Format, { pattern: /[a-zA-Z]/i } );
or at least all non digit characters:
[^0-9] ???
are my regular expressions wrong?
or am i using wrongly the live validation with reg exp?
Thanks!

This regex matches input that is all alphabets.
/^[a-z]+$/i
The + is necessary for it to match multiple characters. Without it, the regex matches the first character and stops.
I recommend this website. It has a lot of samples and you can test the regex.
the i in the regex is the ignoreCase flag. When specified, matches are made irrespective of case.

Related

Regex to match text from multiple links

How to extract links which contain a certain word?
For e.g.:
https://www.test.com/text/1###https://www.test.com/text/word/2###https://www.test.com/text/text/word/3###https://www.test.com/3/text###https://www.test.com/word/3/text/text
How to search "word" from below regex?
((https:).*?(###))
The result should be like this
https://www.test.com/text/word/2
https://www.test.com/text/text/word/3
https://www.test.com/word/3/text/text
Let's try to build such regex. First we need to find the beginning of url:
/(https?:\/\//
We add ? after https for http urls.
Then we need to find any text except ###, so we need to add:
(?:(?!###).)*
which means - any amount of characters not starting a ### sequence.
Also we need to add word itself and previous sub-expression again, since word can be surrounded by any text:
word(?:(?!###).)*
But the thing is that last sub-expression will skip last character before ###, so we need to add one more thing to handle it:
.(?=###|$)
which means - any character followed by ### or end of string. The final expression will look like:
/(https:\/\/(?:(?!###).)*word(?:(?!###).)*.(?=###|$))/g
But i believe, it's better to just split text by ### and then check for needed word by String.prototype.includes.
If the word has to be a part of the pathname, you might use filter in combination with URL and check if the parts of the pathname contain word.
let str = 'https://www.test.com/text/1###https://www.test.com/text/word/2###https://www.test.com/text/text/word/3###https://www.test.com/3/text###https://www.test.com/word/3/text/text';
let filteredUrls = str.split("###")
.filter(s =>
new URL(s).pathname
.split('/')
.includes('word')
);
console.log(filteredUrls);
If you want to use regex only and possessive quantifiers are supported (The javascript tag has been removed) you might use:
https?://[^#w]*(?:#(?!##)|w(?!ord)|[^#w]*)++word.*?(?=###|$)
Regex demo
Previous answer
You for sure looking for this regular expression:
https://www.test.com/(text/)*word/\d+(/text)*
Here is how you can use it in JavaScript context (very slash / is escaped by backslash \/):
var str = 'https://www.test.com/text/1###https://www.test.com/text/word/2###https://www.test.com/text/text/word/3###https://www.test.com/3/text###https://www.test.com/word/3/text/text';
var urls = str.match(/https:\/\/www.test.com\/(text\/)*word\/\d+(\/text)*/g);
console.log(urls);
In the array you get exactly the elements you wanted.
Update the answer after update question and adding comment by the author
If you need take the words from your example string, then you have to use a little more complex regular exception:
var str = 'https://www.test.com/text/1###https://www.test.com/text/word/2###https://www.test.com/text/text/word/3###https://www.test.com/3/text###https://www.test.com/word/3/text/text';
var urls = str.match(/(?<=\/)\w+(?=\/\d+\/\w)|(?<=(\w\/\w+\/))\w+(?=\/\d)/g);
console.log(urls);
Explanation
Here is regular expression /(?<=(\w\/\w+\/))\w+(?=\/\d)|(?<=\/)\w+(?=\/\d+\/\w)/g, limited by /.../ and with the g flag forcing pattern searches for occurrence.
The regular expression has two alternatives ...|...
The first one (?<=\/)\w+(?=\/\d+\/\w) captures cases when the searched word is directly behind the slash (?<=\/) and before more words behind the number (?=\/\d+\/\w).
https://www.test.com/word/3/text/text
The second alternative (?<=(\w\/\w+\/))\w+(?=\/\d) captures cases where the word is preceded by other words following the domain (?<=(\w\/\w+\/)) (in fact two slashes separated by alphanumeric characters) and the searched word is immediately before the slash followed by the number (?=\/\d).
https://www.test.com/text/word/2
https://www.test.com/text/text/word/3
All slashes must be escaped: \/.
The construction (?<=...) means lookbehind in regular expressions and (?=...) means lookahead in regular expressions.
Note 1. The above example currently only works well in a Chrome browser, as that:
(...) now lookbehind is part of the ECMAScript 2018 specification. As of this writing (late 2018), Google's Chrome browser is the only popular JavaScript implementation that supports lookbehind. So if cross-browser compatibility matters, you can't use lookbehind in JavaScript.
Note 2. Lookbehnd, even if it is interpreted correctly, in most regular expression engines must contain a fixed length regular expression, which I do not keep in the example above, because this one is still valid and works for regular expression engines used in Google Chrome's JavaScript engine, JGsoft engine and .NET framework RegEx classes.
Note 3. The lookbehind syntax or its poorer \K replacement are widely supported by many regular expression engines used in a large group of programming languages.
More explanation about regular expressions which I used you can find for example here.
You may first split by ### then check whether /word/ exists in each element:
var s = 'https://www.test.com/text/1###https://www.test.com/text/word/2###https://www.test.com/text/text/word/3###https://www.test.com/3/text###https://www.test.com/word/3/text/text';
var result = [];
s.split(/###/).forEach(function(el) {
if (el.includes('/word/'))
result.push(el);
})
// or else by using filter
// result = s.split(/###/).filter(el => el.includes('/word/'))
console.log(result);

Extract only the First set of numbers in a String

I have a string like below:
input = Test_8234_and_2345_end
REG_EXTRACT(input,'(\d+)',1)
I'm trying to create a regex that targets only the first set of numbers (i.e 8234) in the above string but it it returns none. what is wrong with my above code.
I'm not familiar with Informatica, but the following Regex pattern should work if you disable the greedy global modifier for the pattern:
(?<=_)[0-9]*
I used the regex101.com online regex calculator to test the pattern and it worked with Python.

Regular Expresion in Tableau returns only Null's in Calculated Field

I'm trying to extratct in Tableau the first occurance of part of speech name (e.g. subst, adj, fin) located between { and : in every line from column below:
{subst:pl:nom:m3=18, subst:pl:voc:m3=1, subst:pl:acc:m3=5}
{subst:sg:gen:m3=5, subst:sg:inst:m3=1, subst:sg:gen:f=1, subst:sg:nom:m3=1}
{subst:sg:nom:f=3, subst:sg:loc:f=2, subst:sg:inst:f=1, subst:sg:nom:m3=1}
{adj:sg:nom:m3:pos=2, adj:sg:acc:m3:pos=1, adj:sg:acc:n1.n2:pos=3, adj:pl:acc:m1.p1:pos=3, adj:sg:nom:f:pos=1}
{adj:sg:gen:f:pos=2, adj:sg:nom:n:pos=1}
{fin:sg:ter:imperf=5}
To do this I use the following regular expression: {(\w+):(?:.*?)}$. Unfortunately my calculated field returns only Null's:
Screeen from Tableau
I checked my regular expression on regex tester and is valid:
Sreen from regex101.com
I don't know what I'm doing wrong so if anybody has any suggestions I would be greatfull.
Tableau regex engine is ICU, and there are some differences between it and PCRE.
One of them is that braces that should be matched as literal symbols must be escsaped.
Your regex also contains a redundant non-capturing group ((?:.*?) = .*?) and a lazy quantifier that slows down matching since you want to check for a } at the end of the string, and thus should be changed to a greedy .*.
You can use
REGEXP_EXTRACT([col], '^\{(\w+):.*\}$')

Regular Expression to unmatch a particular string

I am trying to use regular expression in Jmeter where in I need to unmatch a particular string. Here is my input test string : <activationCode>insvn</activationCode>
I need to extract the code insvn from it. I tried using the expression :
[^/<activationCode>]\w+, but does not yield the required code. I am a newbie to regular expression and i need help with this.
Can you use look-behind assertion in jmeter? If so, you can use thatr regex which will give you a word that follows <activationCode>
(?<=\<activationCode\>)\w+
If your input string is encoded (e.g for HTML), use:
(?<=\<activationCode\>)\w+
When designing a regular expression in any language for something like this you can match your input string as three groups: (the opening tag, the content, and the closing tag) then select the content from the second group.

QRegExp Pattern For URLs

I am trying to match google urls from some text that is stored in a variable, using the pattern below.
The urls use double quotes
QRegExp regExp;
regExp.setPattern("http://www.google.com/(.*)");
I manage to match the url but it unwontedly matches all of the text that is contained after it. I have tried using similar variants like the ones below, but they don't seem to work.
regExp.setPattern("http://www.google.com/(.*)\"is");
regExp.setPattern("http://www.google.com/^(.*)$\"");
Any help to get a regular expression that matches just the url alone.
Thanks in advance
Is there a reason you need/want to use a QRegExp?
You could use a QUrl most likely.
Even though it is impossible for us to know what is around the urls in your text (quotes ? parenthesis ? white spaces ?), we can create a better regular expression by trying to do a negative match of characters that cannot be part of the url:
QRegExp regExp;
regExp.setPattern("http://www.google.com/([^()\"' ]*)");
Then you just need to add more possible characters to this negative character class.