I need a regex able to match everything but a string starting with a specific pattern (specifically index.php
and what follows, like index.php?id=2342343
)
This question is related to
regex
I need a regex able to match everything but except a string starting with
index.php
a specific pattern (specifically index.php and what follows, like index.php?id=2342343)
Use method Exec
let match,_x000D_
arr = [],_x000D_
myRe = /([\s\S]+?)(?:index\.php\?id.+)/g;_x000D_
_x000D_
var str = 'http://regular-viragenia/index.php?id=2342343';_x000D_
_x000D_
while ((match = myRe.exec(str)) != null) {_x000D_
arr.push(match[1]);_x000D_
} _x000D_
_x000D_
console.log(arr);
_x000D_
var myRe = /([\s\S]+?)(?:index\.php\?id=.+)/g;_x000D_
var str = 'http://regular-viragenia/index.php?id=2342343';_x000D_
var matches_array = myRe.exec(str);_x000D_
console.log(matches_array[1]);
_x000D_
OR OTHER MATCH
let match,_x000D_
arr = [],_x000D_
myRe = /index.php\?id=((?:(?!index)[\s\S])*)/g;_x000D_
_x000D_
var str = 'http://regular-viragenia/index.php?id=2342343index.php?id=111index.php?id=222';_x000D_
_x000D_
while ((match = myRe.exec(str)) != null) {_x000D_
arr.push(match[1]);_x000D_
} _x000D_
_x000D_
console.log(arr);
_x000D_
How about not using regex:
// In PHP
0 !== strpos($string, 'index.php')
Just match /^index\.php/
then reject whatever matches it.
You can put a ^
in the beginning of a character set to match anything but those characters.
[^=]*
will match everything but =
In python:
>>> import re
>>> p='^(?!index\.php\?[0-9]+).*$'
>>> s1='index.php?12345'
>>> re.match(p,s1)
>>> s2='index.html?12345'
>>> re.match(p,s2)
<_sre.SRE_Match object at 0xb7d65fa8>
Regex: match everything but:
foo
):
world.
at the end):
foo
) (no POSIX compliant patern, sorry):
|
symbol):
foo
):
cat
): /cat(*SKIP)(*FAIL)|[^c]*(?:c(?!at)[^c]*)*/i
or /cat(*SKIP)(*FAIL)|(?:(?!cat).)+/is
(cat)|[^c]*(?:c(?!at)[^c]*)*
(or (?s)(cat)|(?:(?!cat).)*
, or (cat)|[^c]+(?:c(?!at)[^c]*)*|(?:c(?!at)[^c]*)+[^c]*
) and then check with language means: if Group 1 matched, it is not what we need, else, grab the match value if not empty[^a-z]+
(any char other than a lowercase ASCII letter)|
: [^|]+
Demo note: the newline \n
is used inside negated character classes in demos to avoid match overflow to the neighboring line(s). They are not necessary when testing individual strings.
Anchor note: In many languages, use \A
to define the unambiguous start of string, and \z
(in Python, it is \Z
, in JavaScript, $
is OK) to define the very end of the string.
Dot note: In many flavors (but not POSIX, TRE, TCL), .
matches any char but a newline char. Make sure you use a corresponding DOTALL modifier (/s
in PCRE/Boost/.NET/Python/Java and /m
in Ruby) for the .
to match any char including a newline.
Backslash note: In languages where you have to declare patterns with C strings allowing escape sequences (like \n
for a newline), you need to double the backslashes escaping special characters so that the engine could treat them as literal characters (e.g. in Java, world\.
will be declared as "world\\."
, or use a character class: "world[.]"
). Use raw string literals (Python r'\bworld\b'
), C# verbatim string literals @"world\."
, or slashy strings/regex literal notations like /world\./
.
Source: Stackoverflow.com