Regex to match four blocks ranges [duplicate] - php

What is the regular expression (in JavaScript if it matters) to only match if the text is an exact match? That is, there should be no extra characters at other end of the string.
For example, if I'm trying to match for abc, then 1abc1, 1abc, and abc1 would not match.

Use the start and end delimiters: ^abc$

It depends. You could
string.match(/^abc$/)
But that would not match the following string: 'the first 3 letters of the alphabet are abc. not abc123'
I think you would want to use \b (word boundaries):
var str = 'the first 3 letters of the alphabet are abc. not abc123';
var pat = /\b(abc)\b/g;
console.log(str.match(pat));
Live example: http://jsfiddle.net/uu5VJ/
If the former solution works for you, I would advise against using it.
That means you may have something like the following:
var strs = ['abc', 'abc1', 'abc2']
for (var i = 0; i < strs.length; i++) {
if (strs[i] == 'abc') {
//do something
}
else {
//do something else
}
}
While you could use
if (str[i].match(/^abc$/g)) {
//do something
}
It would be considerably more resource-intensive. For me, a general rule of thumb is for a simple string comparison use a conditional expression, for a more dynamic pattern use a regular expression.
More on JavaScript regexes: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

"^" For the begining of the line "$" for the end of it. Eg.:
var re = /^abc$/;
Would match "abc" but not "1abc" or "abc1". You can learn more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Related

PHP Regex: match specific length of string on repeated char [duplicate]

I am trying to write a regular expression which returns a string which is between parentheses. For example: I want to get the string which resides between the strings "(" and ")"
I expect five hundred dollars ($500).
would return
$500
Found Regular expression to get a string between two strings in Javascript
I don't know how to use '(', ')' in regexp.
You need to create a set of escaped (with \) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec("I expect five hundred dollars ($500).");
//matches[1] contains the value between the parentheses
console.log(matches[1]);
Breakdown:
\( : match an opening parentheses
( : begin capturing group
[^)]+: match one or more non ) characters
) : end capturing group
\) : match closing parentheses
Here is a visual explanation on RegExplained
Try string manipulation:
var txt = "I expect five hundred dollars ($500). and new brackets ($600)";
var newTxt = txt.split('(');
for (var i = 1; i < newTxt.length; i++) {
console.log(newTxt[i].split(')')[0]);
}
or regex (which is somewhat slow compare to the above)
var txt = "I expect five hundred dollars ($500). and new brackets ($600)";
var regExp = /\(([^)]+)\)/g;
var matches = txt.match(regExp);
for (var i = 0; i < matches.length; i++) {
var str = matches[i];
console.log(str.substring(1, str.length - 1));
}
Simple solution
Notice: this solution can be used for strings having only single "(" and ")" like string in this question.
("I expect five hundred dollars ($500).").match(/\((.*)\)/).pop();
Online demo (jsfiddle)
To match a substring inside parentheses excluding any inner parentheses you may use
\(([^()]*)\)
pattern. See the regex demo.
In JavaScript, use it like
var rx = /\(([^()]*)\)/g;
Pattern details
\( - a ( char
([^()]*) - Capturing group 1: a negated character class matching any 0 or more chars other than ( and )
\) - a ) char.
To get the whole match, grab Group 0 value, if you need the text inside parentheses, grab Group 1 value.
Most up-to-date JavaScript code demo (using matchAll):
const strs = ["I expect five hundred dollars ($500).", "I expect.. :( five hundred dollars ($500)."];
const rx = /\(([^()]*)\)/g;
strs.forEach(x => {
const matches = [...x.matchAll(rx)];
console.log( Array.from(matches, m => m[0]) ); // All full match values
console.log( Array.from(matches, m => m[1]) ); // All Group 1 values
});
Legacy JavaScript code demo (ES5 compliant):
var strs = ["I expect five hundred dollars ($500).", "I expect.. :( five hundred dollars ($500)."];
var rx = /\(([^()]*)\)/g;
for (var i=0;i<strs.length;i++) {
console.log(strs[i]);
// Grab Group 1 values:
var res=[], m;
while(m=rx.exec(strs[i])) {
res.push(m[1]);
}
console.log("Group 1: ", res);
// Grab whole values
console.log("Whole matches: ", strs[i].match(rx));
}
Ported Mr_Green's answer to a functional programming style to avoid use of temporary global variables.
var matches = string2.split('[')
.filter(function(v){ return v.indexOf(']') > -1})
.map( function(value) {
return value.split(']')[0]
})
Alternative:
var str = "I expect five hundred dollars ($500) ($1).";
str.match(/\(.*?\)/g).map(x => x.replace(/[()]/g, ""));
→ (2) ["$500", "$1"]
It is possible to replace brackets with square or curly brackets if you need
For just digits after a currency sign : \(.+\s*\d+\s*\) should work
Or \(.+\) for anything inside brackets
let str = "Before brackets (Inside brackets) After brackets".replace(/.*\(|\).*/g, '');
console.log(str) // Inside brackets
var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/;
alert(rex.exec(str));
Will match the first number starting with a $ and followed by ')'. ')' will not be part of the match. The code alerts with the first match.
var str = "I expect five hundred dollars ($500) ($1).";
var rex = /\$\d+(?=\))/g;
var matches = str.match(rex);
for (var i = 0; i < matches.length; i++)
{
alert(matches[i]);
}
This code alerts with all the matches.
References:
search for "?=n"
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
search for "x(?=y)"
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
Simple:
(?<value>(?<=\().*(?=\)))
I hope I've helped.

Why does my regex group quantifier not work?

I try to write some regex for Dutch license plates (kentekens), the documentation is very clear and I only want to check them on format, not if the actual alpha character is possible for now.
My regex (regex101) looks as follows:
(([0-9]{1,2}|[a-z]{1,3})-([0-9]{2,3}|[a-z]{2,3})-([0-9]{1,2}|[a-z]{1,2})){8}/gi
However this returns no matched, while
([0-9]{1,2}|[a-z]{1,3})-([0-9]{2,3}|[a-z]{2,3})-([0-9]{1,2}|[a-z]{1,2}/gi
does
However I do like to check the total length as well.
JS Demo snippet
const regex = /([0-9]{1,2}|[a-z]{1,3})-([0-9]{2,3}|[a-z]{2,3})-([0-9]{1,2}|[a-z]{1,2})/gi;
const str = `XX-99-99
2​ 1965​ 99-99-XX ​
3​ 1973​ 99-XX-99​
4​ 1978​ XX-99-XX ​
5​ 1991​ XX-XX-99 ​
6​ 1999​ 99-XX-XX ​
7​ 2005​ 99-XXX-9​
8​ 2009​ 9-XXX-99​
9​ 2006​ XX-999-X ​
10​ 2008​ X-999-XX ​
​11 ​2015 ​XXX-99-X`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
This is because {8} quantifier added at the end will act on previous expression, in this case the whole regex, because it is enclosed with parenthesis. See here what matches this regex.
To test for length, use this regex (?=^.{1,8}$)(([0-9]{1,2}|[a-z]{1,3})-([0-9]{2,3}|[a-z]{2,3})-([0-9]{1,2}|[a-z]{1,2})) It uses a lookahead to make sure that the following characters match ^.{1,8}$, which means the whole string should contain between 1 and 8 character, you can adjust it to your needs.

Regex to match blocks of text containing substrings wrapped in triple square brackets

Given a whole block of text:
Welcome to [[[RegExr v2.0 by gskinner.com]]]
Edit the Expression & Text to see matches. Roll over matches or the
expression for details. Undo mistakes with ctrl-z. Save & Share
expressions with friends or the Community. [[[A full Reference & Help is
available in the Library, or watch the video Tutorial.
Sample text for]]] testing: abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 _+-.,!##$%^&*();/|<>"' 12345
-98.7 3.141 .6180 9,000 +42
555.123.4567 +1-(800)-555-2468 foo#demo.net bar.ba#test.co.uk www.demo.com
I need a regex that can validate that all open triple square brackets '[[[' in the string are paired up and closed ']]]'. Nested brackets and strings that begin with ']]]' or end with '[[[' should return false.
I know there are ways to loop through the string and evaluate this, but I might be potentially dealing with very large strings of text and was hoping a regex would be faster/better for performance.
Thanks.
I've come up with the following solution using the pattern: /[\[]{3}[^\[\]]*[\]]{3}/. Unfortunately the third $text case will still return false, so I'm working on that. The regex pattern can be seen in action here.
$text = 'Some [[[default]]] [[[text]]] here'; //valid
//$text = 'Some [[[default text [[[here]]]'; //invalid
//$text = 'Some [[[default text [here]]]'; //invalid
// Get the number of opening and closing brackets
$open_bracket_count = substr_count($text, '[[[');
$close_bracket_count = substr_count($text, ']]]');
// Check if number of '[[[' is same as ']]]'
if ($open_bracket_count === $close_bracket_count)
{
// Match valid bracketed substrings in the text
$validation_pattern = '/[\[]{3}[^\[\]]*[\]]{3}/';
$valid_match_count = preg_match_all($validation_pattern, $text, $valid_matches);
// Valid matches should equal the number of substrings attempting to be wrapped in brackets
if ($valid_match_count === $open_bracket_count)
{
return true;
}
else
{
return false;
}
}
// If not equal, we know right away the string contains invalid brackets
else
{
return false;
}

Just remove a a-zA-z section in regular expression

This is my regex string:
'(?!('.$exceptions.')((\W+)|$))[a-zA-Z\-_]+/?$'
$exceptions is a variable contains a string like this :
word1|word2|word3|word4|wordN
I just want to remove the section a-zA-Z which means I want to just delete the rule which checking english chars, because of unicode.
A sample :
$exception ='word1|word3|word3|word4' ;
$myword = 'a-unicode-statement-like-سلام' ;
If $myword compared with the regex rules string it will not match that because of سلام
it is not in a-zA-z range i just want remove this limitation (a-zA-Z)
Try adding something to match everything else, instead of your a-zA-Z rule.
'(?!('.$exceptions.')(.*))'
EDIT:
After reading your comment below. Maybe a better solution is to use the one proposed for this question: wordpress: how to check if the slug contains a specific word?
You can then check using something like this:
$url = $_SERVER["REQUEST_URI"];
$isException = strpos($url, 'word1');
if ($isException !== false)
{
//url contains word in exceptions!
}
From what I understand, I think you're looking for this:
$exceptions = ["word1","word2","word3"];
// or $exceptions = explode("|",$exceptions) to work with what you have already
if( in_array($string,$exceptions)) {
// word is in exceptions
}

preg_match php to match javascript

i have this function in php :
$html_price = "text text 12 eur or $ 22,01 text text";
preg_match_all('/(?<=|^)(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)(?:\ |)(?:\$|usd|eur)+(?=|$)|(?:\$|usd|eur)(?:| )(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)/', strtolower($html_price), $price_array1);
print_r($price_array1);
Now i want use in javascript the same regex but i have an error:
SyntaxError: invalid quantifier
my javascript :
maxime_string = "((?<=|^)(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?)(?:\ |)(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)+(?=|$)|(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)(?:| )(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?))";
maxime_regex = new RegExp(maxime_string);
var text = "text text 12 eur or $ 22,01 text text";
var result = text.match(maxime_regex);
var max = result.length;
alert(result.length);
for (i = 0; i < max; i++)
{
document.write(result[i] + "<br/>");
}
Can you help me ?
JavaScript does not support lookbehind - that's probably where you got your error from. Some things:
(?<=|^) makes no sense. It's a lookbehind demanding either nothing or string begin to come before this - it's always true. And if you want to match the string start, use a single ^.
(?:.[0-9]*)? - I guess you wanted to escape the dot so it matches literally
(?:\ |): You do not need to escape blanks. And instead of an alternative "nothing", you should just make the blank optional: " ?".
(?=|$) makes as much sense as the first group.
€ is a broken unicode character?
Also, you should consider using a regex literal instead of the RegExp constructor with a string literal - it eats one level of backslash escaping. And it seems you're missing the global flag, so that you get back all matches. Try this:
var maxime_regex = /((?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:\.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?) ?(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€)+|(?:\$|usd|eur|euro|euros|firm|obro|€|£|gbp|dollar|aud|cdn|sgd|€) ?(?:[0-9]{1,3}(?:,| ?[0-9]{3})*(?:\.[0-9]*)?|[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9] ​*)?))/g
This post does not include any effort to understand, optimize or shorten that regex monster

Categories