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]+)*$
Related
I have this few strings:
'user/1/myid_print'
'user/2/myid_print'
'user/3/myid_print'
'user/4/myid_print'
'user/5/myid_print'
The second part is the dynamic one which must contain only integers. What is it's regular expression?
Try this:
/user\/\d+\/myid_print/
the \d+ matches a number which contains at least one digit.
if it is a non-zero number, replace the \d+ with [1-9]\d*
It depends a bit on what language you're using, but one valid answer for Python is:
user/\d/myid_print
The / character is often used in describing regular expressions and sometimes needs \ before it to make it match part of the string, a valid answer might be:
user\/\d\/myid_print
These match the text "user/" and "/myid_print" literally, and \d matches the pattern "any digit 0-9". If you need to match the numbers 1,2,3,4,5 only, then use [1-5] instead of \d
Test it here: https://regex101.com/r/mS4xN4/1
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
I'm trying to write a simple regular expression that recognizes a sequence of characters that are not columns or are escaped columns.
I.e:
foo:bar //Does not match
but
foo\:bar //Does match
By my knowledge of Regular Languages, such language can be described by the regular expression
/([^:]|\\[:])*/
You can see a graphical representation of this expression in the wonderful tool Regexper
Using php's preg_match (that is based on the PCRE engine), such expression does not match "foo\:bar".
However, if substitute the class with the single char:
/([^:]|\\:)*/
the expression matches.
Do you have an explanation for this? Is this a sort of limitation of the PCRE engine on character classes?
PS: Testing the first expression on RegExr, that is based on AS3 Regexp engine, does not offer a match, while changing the alternation order:
/(\\[:]|[^:])*/
it does match, while the same expression does not match in PCRE.
preg_match() accepts a regular expression pattern as a string, so you need to double escape everything.
^(?:[^:\\\\]|\\\\:)+$
This matches one or more characters that are not colons or escape characters [^:\\\\], or an escaped colon \\\\:.
Why your first regular expression didn't work: /([^:]|\\[:])*/.
This matches a non-colon [^:], or it matches \\[:] which matches a literal [ followed by a literal : and then a literal ].
Why this works : /([^:]|\\:)*/ ?
This matches a non-colon [^:], or it matches a literal \\: so it effectively matches everything.
Edit: Why /([^:]|E[:])*/ won't match fooE:bar ?
This is what happens: [^:] matches the f then it matches o then the other o then it matches the E, now it finds a colon : and it can't match it, but since by default the PCRE engine doesn't look for the longest possible match it is satisfied with what is has matched so far and stops right there and returns fooE as a match without trying the other alternative E[:] (which is equal by the way to E:) at all.
If you want to match the entire sequence then you will to use an expression like this one:
/([^:E]|E[:])*/
This prevents [^:] from consuming that E.
You can try this. This allow the secuence \\: to have a chance before the negated character class [^:].
^(?:\\:|[^:])+$
If you use the values in the alternation bar inverted as in ^((?:[^:]|\\:)+$ it will not match escaped colon \: because the first alternative will consume the slash (\) before the second expression have a chance to try.
I have a function that searches a string for a letter and/or number combination which works well, except sometimes that combination also contains a hyphen which I would like to match too. The regex below is taken from within a larger regex pattern match, but this is the part of the expression that i am stuck on.
For example:
([A-Za-z0-9-]+)
Matches the string 123 or 123A but it does not match 123-125 or 123A-125A
I'm sure it is a simple solution but it's not my forte.
thanks in advance
$key= preg_replace("/http:\/\/www.website.com\/[0-9]+\/([A-Za-z0-9-]+)_([A-Za-z_]+)_(MUSIC|VIDEO|PHOTOS)_([A-Z_]+)_(..+)_([0-9]+)/i", '${1}, ${2}, ${3}, ${4}, ${5}, ${6}', $url);
simple, you need to escape the hyphen in your character set:
([A-Za-z0-9\-]+)
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.