Regex - Invalid target for quantifier - php

I have this simple regular expression, and I'm testing it on RegExr.
^(?<name>[a-z0-9\-]+)
It should give me an associative array with a name field that matches strings that contains a-z and 0-9.
But I get the ? character underlined in red with that error.
Why?

Well unfortunately, RegExr v2 is dependent on the JS RegExp implementation, which does not support named capture groups. See your working regular expression at regular expressions 101

Try another regex site:
^(?<name>[a-z0-9\-]+)
Debuggex Demo

Related

REGEX PHP ANY DIGITS QUANTITY 0-9

I need to validate a regex where between STRING_{here}G_ can be 0 or even 4 digits, I tried the following regex:
(?<=TEST_[0-9]{0,4}G_).*
But the tester returns the error:
Your pattern contains one or more errors, please see the explanation section above.
And when trying to use manually, using two [0-9], it doesn't get my strings
ABC_TEST_20G_a123-abc1
ABC_TEST_100G_abc1
I need a regex that validates both strings and returns what is after G_
Remembering that the regex must have the "TEST_", it is a string that I need to validate
Most regexp engines don't allow lookbehinds to be variable-length, so you can't have a {0,4} quantifier in it.
Instead of a lookbehind, use a capture group to capture everything after this pattern.
TEST_[0-9]{0,4}G_(.*)
Capture group 1 will contain what you want to get.
DEMO

Regular expression that matches a string having alphabets or alphanumeric only

I am currently using a regular expression that accepts only alphabets, like
?:[a-z][a-z]+
I have also used a regular expression that accepts alphanumeric but it does not meet my requirements
My strings are in following pattern
"john" or "john123"
Any kind of help would be appreciated
try ^[a-zA-Z0-9]+$, this one works for me
Try using this ^([A-Za-z]|[0-9])+$
You can use (:?[a-z0-9][a-z0-9]*) 'non capturing' or ([a-z0-9][a-z0-9]*) 'capturing' in case insensitive mode to match alphanumeric words depending on whether you want to capture the words matched by regular expression.
this regular expression accept alphabets at the beginning of your String and alphanumeric at the end
you can test it in https://regex101.com/
^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$

PHP regular expression issue preg_match() on timestamps

Facing issue with regular expression
2013-05-29 15:15:12 string I am matching with /^(\d{4})-(\d{2})-(\d{2})({\s}+(\d{2}):(\d{2}):(\d{2}))?$/ with preg_match but not validating ... its giving false.
What should be regexp to match 2013-05-29 15:15:12 or 2013-05-29 pattern.
Let's take a look at your regex first. Between the date and the time you're matching {\s}+. This means "the character {, followed by a space/tab, followed by one or more }'s".
Replace {\s} with ?:\s+ (a non capturing group matching one or more spaces/tabs) so the full regex is
^(\d{4})-(\d{2})-(\d{2})(?:\s+(\d{2}):(\d{2}):(\d{2}))?$
DEMO
The {\s}+ is wrong. It should be \s+. The curly brackets are used as quantifiers or literals only.

what does the regular expression (?<!-) mean

I'm trying to understand a piece of code and came across this regular expression used in PHP's preg_replace function.
'/(?<!-)color[^{:]*:[^{#]*$/i'
This bit... (?<!-)
doesnt appear in any of my reg-exp manuals. Anyone know what this means please? (Google doesnt return anything - I dont think symbols work in google.)
The ?<! at the start of a parenthetical group is a negative lookbehind. It asserts that the word color (strictly, the c in the engine) was not preceded by a - character.
So, for a more concrete example, it would match color in the strings:
color
+color
someTextColor
But it will fail on something like -color or background-color. Also note that the engine will not technically "match" whatever precedes the c, it simply asserts that it is not a hyphen. This can be an important distinction depending on the context (illustrated on Rubular with a trivial example; note that only the b in the last string is matched, not the preceding letter).
PHP uses perl compatible regular expressions (PCRE) for the preg_* functions. From perldoc perlre:
"(?<!pattern)"
A zero-width negative look-behind assertion. For example
"/(?<!bar)foo/" matches any occurrence of "foo" that does
not follow "bar". Works only for fixed-width look-
behind.
I'm learning regular expressions using Python's re module!
http://docs.python.org/library/re.html
Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

PHP Regex Question

I am developing an application using PHP but I am new to regular expressions, I could not find a solution to my problem. I want to replace all occurences of #word with a link, i have written a preg_match for this:
$text=preg_replace('~#([\p{L}|\p{N}]+)~u', '#$1', $text);
The problem is, this regular expression also matches the html character codes like
'
and gives corrupt output. I need to exclude the words starting with &# but i do not know how to do that using regular expressions.
Thanks for your help.
'~(?<!&)#([\p{L}|\p{N}]+)~u'
That's a negative lookbehind assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
Matches # only if not preceded by &
http://gskinner.com/RegExr/
use this online regular expression constructor. They have explanation for every flag you may want to use.. and you will see highlighted matches in example text.
and yes use [a-zA-Z]
You would need to add a [A-Za-z] rule in your regular expression statement so that it only limits itself to letters and no numbers.
I will edit with an example later on.

Categories