I'm trying to extract the following pattern {#56DS1e5R9w7v} which is :
{
Hash
a-z, A-Z, 0-9 ( not necessarily an alphanumeric string )
}
Any ideas please?
Thank you
Try this pattern:
\{#([^}]*)\}
It should match all characters that are not }, and place the result in a captured group. You may want to change [^}]* to \w* or [A-Za-z0-9]* if that's problematic.
Example (also on ideone.com):
$str = "hello {#56DS1e5R9w7v} good people";
preg_match_all("/\{#([^}]*)\}/", $str, $matches);
Something like ({#[a-z0-9]+?}) ?
preg_match('/(\{#[a-z0-9]+?\})/i', $sString)
What about:
<?php
$code = '{#56DS1e5R9w7v}';
$matches = preg_match('/^\{#[a-zA-Z0-9]+\}$/', $code);
?>
This makes sure the string begins with a { the second char must be a #, the 3th char until } must be alpha numeric and it must end with a }.
Hope this helped!
Related
If the first character of my string contains any of the following letters, then I would like to change the first letter to Uppercase: (a,b,c,d,f,g,h,j,k,l,m,n,o,p,q,r,s,t,v,w,y,z) but not (e,i,u,x).
For example,
luke would become Luke
egg would stay the same as egg
dragon would become Dragon
I am trying to acheive this with PHP, here's what I have so far:
<?php if($str("t","t"))
echo ucfirst($str);
else
echo "False";
?>
My code is simply wrong and it doesn't work and I would be really grateful for some help.
Without regex:
function ucfirstWithCond($str){
$exclude = array('e','i','u','x');
if(!in_array(substr($str, 0, 1), $exclude)){
return ucfirst($str);
}
return $str;
}
$test = "egg";
var_dump(ucfirstWithCond($test)); //egg
$test = "luke";
var_dump(ucfirstWithCond($test)); //Luke
Demo:
http://sandbox.onlinephpfunctions.com/code/c87c6cbf8c616dd76fe69b8f081a1fbf61cf2148
You may use
$str = preg_replace_callback('~^(?![eiux])[a-z]~', function($m) {
return ucfirst($m[0]);
}, $str);
See the PHP demo
The ^(?![eiux])[a-z] regex matches any lowercase ASCII char at the start of the string but e, u, i and x and the letter matched is turned to upper inside the callback function to preg_replace_callback.
If you plan to process each word in a string you need to replace ^ with \b, or - to support hyphenated words - with \b(?<!-) or even with (?<!\S) (to require a space or start of string before the word).
If the first character could be other than a letter then check with an array range from a-z that excludes e,i,u,x:
if(in_array($str[0], array_diff(range('a','z'), ['e','i','u','x']))) {
$str[0] = ucfirst($str[0]);
}
Probably simpler to just check for the excluded characters:
if(!in_array($str[0], ['e','i','u','x'])) {
$str[0] = ucfirst($str[0]);
}
I need to check to see if a variable contains anything OTHER than 0-9 and the "-" and the "+" character and the " "(space).
The preg_match I have written does not work. Any help would be appreciated.
<?php
$var="+91 9766554433";
if(preg_match('/[0-9 +\-]/i', $var))
echo $var;
?>
You have to add a * as a quantifier to the whole character class and add anchors to the start and end of the regex: ^ and $ means to match only lines containing nothing but the inner regex from from start to end of line. Also, the i modifier is unnecessary since there is no need for case-insensitivity in this regex.
This should do the work.
if(!preg_match('/^[0-9 +-]*$/', $var)){
//variable contains char not allowed
}else{
//variable only contains allowed chars
}
Just negate the character class:
if ( preg_match('/[^0-9 +-]/', $var) )
echo $var;
or add anchors and quantifier:
if ( preg_match('/^[0-9 +-]+$/', $var) )
echo $var;
The case insensitive modifier is not mandatory in your case.
You can try regex101.com to test your regex to match your criteria and then on the left panel, you'll find code generator, which will generate code for PHP, Python, and Javascript.
$re = "/^[\\d\\s\\+\\-]+$/i";
$str = "+91 9766554433";
preg_match($re, $str, $matches);
You can take a look here.
Try see if this works. I haven't gotten around to test it beforehand, so I apologize if it doesn't work.
if(!preg_match('/^[0-9]+.-.+." ".*$/', $var)){
//variable contains char not allowed
}else{
//variable only contains allowed chars
}
I Have one string like below.
$string = "2346#$ABSC$%###234567";
Now I want last character from this string that is not numeric or special character, It should be only A-a to Z-z.
Means, I need only "C" from this string.
I have try this formula:
substr($string, -1);
You should look into regular expressions using something like preg_match()
An expression like this would match:
/([a-z])[^a-z]*$/i
It means:
([a-z]) Capture an a-z character (the i at the end makes it case-insensitive)
[^a-z]*$ followed by 0 or more non a-z characters until the end of the string
See an example.
This should work for you:
(Here I just replace everything expect a-zA-Z with an empty string. After this I just access the last character)
<?php
$string = '2346#$ABSC$%###234567';
$string = preg_replace("/[^a-zA-Z]/", "", $string);
echo $string[strlen($string)-1];
?>
output:
C
The proper regex is: ([a-z])[^a-z]*$
I am trying to verify in PHP with preg_match that an input string contains only "a-z, A-Z, -, _ ,0-9" characters. If it contains just these, then validate.
I tried to search on google but I could not find anything usefull.
Can anybody help?
Thank you !
Use the pattern '/^[A-Za-z0-9_-]*$/', if an empty string is also valid. Otherwise '/^[A-Za-z0-9_-]+$/'
So:
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
Also, note that you want to put a '-' last in the character class as part of the character class, that way it is read as a literal '-' and not the dash between two characters such as the hyphen between A-Z.
$data = 'abc123-_';
echo preg_match('/^[\w|\-]+$/', $data); //match and output 1
$data = 'abc..';
echo preg_match('/^[\w|\-]+$/', $data); //not match and output 0
You can use preg_replace($pattern, $replacement, $subject):
if (preg_replace('/[A-Za-z0-9\-\_]/', '', $string)) {
echo "Detect non valid character inside the string";
}
The idea is to remove any valid chars, if the result is NOT empty do the code.
Here is the regex I currently have (which kind of works):
$regex = '/[\w ]{7,30}/';
My revision looks like what I want, but it does not work at all:
$regex = '^[\w ]{7,30}$';
Here is how I am using the regex:
public function isValid( $value )
{
$regex = '/^[\w ]{7,30}$/';
return preg_match( $regex, $value ) ? true : false;
}
I am trying to match the following:
Any lower/upper case letter
Any digit
Can contain spaces
Cannot contain line breaks or tab space
Minimum of 7 characters
Maximum of 30 characters
Valid inputs:
Testing
Test ing
Test123
Test 123
Test___
Invalid inputs:
Testing#
Testin8+
Tester1&
The first regex will match all valid inputs, as well as invalid (as long as the first four characters are valid, it doesn't care about the rest). The second regex matches nothing.
Try combining both like so:
$regex = '/^[\w ]{7,30}$/';
Don't forget your delimiters:
/^[\w ]{7,30}$/
You're missing the / at the beginning and end.
$regex = '/^[\w ]{7,30}$/';
'/^[\w ]{7,30}$/'
You're missing the delimiters.