I need do a regex match for ASCII characters 32 - 90 inclusive.
I've attempted the following, however it doesn't seem to return anything.
preg_match("/^[\x20-\x5A]+$/u", $input)
The idea is that it is from hex 20 to hex 5a. I pulled these from http://www.asciitable.com/
I've got a spot for testing this on http://www.phpliveregex.com/p/2Dh
Your current range only supports upper case letters, so you need the /i modifier:
$input = 'adddd ### AAAA????';
preg_match('/^[\x20-\x5A]+$/i', $input); // int(1)
Alternatively, add the extra letters in the range:
preg_match('/^[\x20-\x5A\x61-\x7A]+$/', $input))
You need to use preg_match's third parameter to assign it to a variable
preg_match("/^[\x20-\x5A]+$/u", $input, $matches)
The standard return of this function is a 1 or 0/FALSE
eg...
if(preg_match("/^[\x20-\x5A]+$/u", $input, $matches))
{
var_dump($matches);
}
Related
According to PHP manual "If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on."
How can I return a value from a string with only knowing the first few characters?
The string is dynamic and will always change whats inside, but the first four character will always be the same.
For example how could I return "Car" from this string "TmpsCar". The string will always have "Tmps" followed by something else.
From what I understand I can return using something like this
preg_match('/(Tmps+)/', $fieldName, $matches);
echo($matches[1]);
Should return "Car".
Your regex is flawed. Use this:
preg_match('/^Tmps(.+)$/', $fieldName, $matches);
echo($matches[1]);
$matches = []; // Initialize the matches array first
if (preg_match('/^Tmps(.+)/', $fieldName, $matches)) {
// if the regex matched the input string, echo the first captured group
echo($matches[1]);
}
Note that this task could easily be accomplished without regex at all (with better performance): See startsWith() and endsWith() functions in PHP.
"The string will always have "Tmps" followed by something else."
You don't need a regular expression, in that case.
$result = substr($fieldName, 4);
If the first four characters are always the same, just take the portion of the string after that.
An alternative way is using the explode function
$fieldName= "TmpsCar";
$matches = explode("Tmps", $fieldName);
if(isset($matches[1])){
echo $matches[1]; // return "Car"
}
Given that the text you are looking in, contains more than just a string, starting with Tmps, you might look for the \w+ pattern, which matches any "word" char.
This would result in such an regular expression:
/Tmps(\w+)/
and altogether in php
$text = "This TmpsCars is a test";
if (preg_match('/Tmps(\w+)/', $text, $m)) {
echo "Found:" . $m[1]; // this would return Cars
}
I have a string like this:
[numbers]firstword[numbers]mytargetstring
I would like to know how is it possible to extract "targetstring" taking account the following :
a.) Numbers are numerical digits for example, my complete string with numbers:
12firstword21mytargetstring
b.) Numbers can be any digits, for example above are two digits each, but it can be any number of digits like this:
123firstword21567mytargetstring
Regardless of the number of digits, I am only interested in extracting "mytargetstring".
By the way "firstword" is fixed and will not change with any combination.
I am not very good in Regex so I appreciate someone with strong background can suggest how to do this using PHP. Thank you so much.
This will do it (or should do)
$input = '12firstword21mytargetstring';
preg_match('/\d+\w+\d+(\w+)$/', $input, $matches);
echo $matches[1]; // mytargetstring
It breaks down as
\d+\w+\d+(\w+)$
\d+ - One or more numbers
\w+ - followed by 1 or more word characters
\d+ - followed by 1 or more numbers
(\w+)$ - followed by 1 or more word characters that end the string. The brackets mark this as a group you want to extract
preg_match("/[0-9]+[a-z]+[0-9]+([a-z]+)/i", $your_string, $matches);
print_r($matches);
You can do it with preg_match and pattern syntax.
$string ='2firstword21mytargetstring';
if (preg_match ('/\d(\D*)$/', $string, $match)){
// ^ -- end of string
// ^ -- 0 or more
// ^^ -- any non digit character
// ^^ -- any digit character
var_dump($match[1]);}
Try it like,
print_r(preg_split('/\d+/i', "12firstword21mytargetstring"));
echo '<br/>';
echo 'Final string is: '.end(preg_split('/\d+/i', "12firstword21mytargetstring"));
Tested on http://writecodeonline.com/php/
You don't need regex for that:
for ($i=strlen($string)-1; $i; $i--) {
if (is_numeric($string[$i])) break;
}
$extracted_string = substr($string, $i+1);
Above it's probably the faster implementation you can get, certainly faster than using regex, which you don't need for this simple case.
See the working demo
your simple solution is here :-
$keywords = preg_split("/[\d,]+/", "hypertext123language2434programming");
echo($keywords[2]);
I'm trying to cleanup some data, that has N digits in the begining of the string and some in the rest of it. I need to extract only that N first digits.
Here's an example string
1410{{data}} est un program56me de lв556Ђ™
122 datadatadata5654df sdfs989
123datadatadata5654df sdfs989
I need as result to get
1410,122,123
How about:
$str = preg_replace('/^(\d+).*$/', "$1", $str);
Try using this regex :
^([0-9]+)?
using the preg_match command.
It'll spot consecutive-digit sequences at the beginning of a string. :-)
Example :
function getInitial($line)
{
$regex = "^([0-9]+)?";
preg_match($regex, $line, $match);
return $match[1];
}
This should do the trick: ^(\d+). It will instruct the regex engine to start from the beginning of the string, match one or more digits and put them in a group. Any other characters will be ignored.
As I understand, you need at least N digits at the start of each line:
preg_match_all("/^(\d{3,})/m", $text, $matches, PREG_SET_ORDER);
print_r($matches);
one possible regex could be '#^(\d+)#' : in addition to the php function preg_match('#^(\d+)#',$string,$param); $param[1] will return those numbers
How can I extract hyphenated strings from this string line?
ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service
I just want to extract "ADW-CFS-WE" from it but has been very unsuccessful for the past few hours. I'm stuck with this simple regEx "(.*)" making the all of the string stated about selected.
You can probably use:
preg_match("/\w+(-\w+)+/", ...)
The \w+ will match any number of alphanumeric characters (= one word). And the second group ( ) is any additional number of hyphen with letters.
The trick with regular expressions is often specificity. Using .* will often match too much.
$input = "ADW-CFS-WE X-Y CI SLA Def No SLANAME CI Max Outage Service";
preg_match_all('/[A-Z]+-[A-Z-]+/', $input, $matches);
foreach ($matches[0] as $m) {
echo $matches . "\n";
}
Note that this solutions assumes that only uppercase A-Z can match. If that's not the case, insert the correct character class. For example, if you want to allow arbitrary letters (like a and Ä), replace [A-Z] with \p{L}.
Just catch every space free [^\s] words with at least an '-'.
The following expression will do it:
<?php
$z = "ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service";
$r = preg_match('#([^\s]*-[^\s]*)#', $z, $matches);
var_dump($matches);
The following pattern assumes the data is at the beginning of the string, contains only capitalized letters and may contain a hyphen before each group of one or more of those letters:
<?php
$str = 'ADW-CFS-WE CI SLA Def No SLANAME CI Max Outage Service';
if (preg_match('/^(?:-?[A-Z]+)+/', $str, $matches) !== false)
var_dump($matches);
Result:
array(1) {
[0]=>
string(10) "ADW-CFS-WE"
}
Count character '_' in start line
example :
subject = '_abcd_abc'; // return 1
or
subject = '__abcd_abc'; // return 2
or
subject = '___abcd_abc'; // return 3
everyone help me ~
I use PHP
If you are sure the start of the string contains _, you can do this with just strspn():
echo strspn('___abcd_abc', '_');
// -> 3
If there might be no leading underscores, you can still do this without a regex using strlen and ltrim:
strlen($str) - strlen(ltrim($str, "_"));
This counts the string length, then subtracts the string length without the underscores on the left, the result being the number of underscores.
strspn()
ltrim()
strlen()
Try this:
return preg_match('/^_+/', $str, $match) ? strlen($match[0]) : 0;
If preg_match finds a match, $match[0] will contain that match and strlen($match[0]) returns the length of the match; otherwise the expression will return 0.