Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What can be the regex to validate the string as below
0000-XXXX-XXXX
four digits at start then hyphen then 4 alphabets then hyphen and then 4 alphabets again.
Thank you.
$string="1234-ABCD-EFGH";
preg_match("~[0-9]{4}-[A-Z]{4}-[A-Z]{4}~", $string, $matches);
print_r($matches);
This code will output the $matches array that contains the match information.
preg_match will return true or false depending on whether the string matched.
If you prefer to also match lower case characters, use this one:
preg_match("~[0-9]{4}-[A-Za-z]{4}-[A-Za-z]{4}~", $string, $matches);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i have sequence of strings in excel file.
example:
1780CAR405108CCC72
1780CAR405108KK89.0
1780CAR405108B7888
I need to get the numbers/floats whatever after the last occurrence of an ALPHABET
like in these examples above, after CCC or KK or B. need assistance asap....
preg_match('#[^a-zA-Z]+$#', $string, $result);
[a-zA-Z]([\d\.]+?)$
lazy match anchored at the end of the string, matches digits and fullstops into a capture group.
Also, since you're using PHP, you can use this if you don't want to deal with capture groups:
(?<=[^\d])[\d\.]+?$
You can match
[^a-zA-Z]+$
[^a-zA-Z] is a negated character class, it stands for "one character, any character but the ones inside the class". $ matches the end of the string.
See demo here.
Using preg_match:
preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72", $match);
print($match);
$matches will contain numbers/float after alphabets.
preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72",$matches);
'/pattern/'==> where slash is the delimiter.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I would like to parse a string and get characters from the end back to the first slash in the string. The string may change.
An example may be:
'string4\string3\string2\string1'
And I would want string1 from this example.
Another example string is:
'string3\string2\string1'
And I would like to get string1 from this example. Can this be done in php using regular expressions?
Just break the string apart by the \ character and get the last part:
$parts = explode('\\', $string);
echo $parts[count($parts) - 1];
Demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to validate for UK phone numbers - the specific requirements I have are:
must only consist of digits
the only non-digit characters allowed are space and dash
must begin with a zero
Can anyone suggest a nice and simple regular expression I can use for this?
Try with following regex:
^0([ -]\d+)*\d+$
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
when user submit a form input text
i want it just to allow a-z 0-9 and -_ just this numbers digits and -_
how do i check the input and make it only this inputs allows and delete the rest
Thank You!
$output = preg_replace("/[^a-zA-Z0-9_\-]/", '', $input);
function validate($value){
return preg_match("/^[a-zA-Z0-9_\-]*$/", $value) !== 0;
}
If the return value is false then you have invalid characters, if it true, everything is correct.
$input = preg_replace("/[^0-9a-z\_\-]/i", "", $_POST["myfield"]);
Use regexp.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
$string='A;B;C;1;2-;D'
how can i remove all characters from this the string above but keep the Letters and the ";"
Try this simple regex:
preg_replace('/[^A-Z;]+/', '', $string);
or
preg_replace('/[^A-Z;]+/i', '', $string);
If you need case insensitive.
You can use:
$repl = preg_replace('/[^a-z;]+/i', '', $str);
^a-z; inside square brackets (character class) means match anything but English letters a-z (range) OR a semi-colon ;
/i is for ignore case to avoid matching uppercase/lowercase english letters.
try this
preg_match('/[^a-z;]/i', $string);