I am getting a result as a return of a laravel console command like
Some text as: 'Nerad'
Now i tried
$regex = '/(?<=\bSome text as:\s)(?:[\w-]+)/is';
preg_match_all( $regex, $d, $matches );
but its returning empty.
my guess is something is wrong with single quotes, for this i need to change the regex..
Any guess?
Note that you get no match because the ' before Nerad is not matched, nor checked with the lookbehind.
If you need to check the context, but avoid including it into the match, in PHP regex, it can be done with a \K match reset operator:
$regex = '/\bSome text as:\s*'\K[\w-]+/i';
See the regex demo
The output array structure will be cleaner than when using a capturing group and you may check for unknown width context (lookbehind patterns are fixed width in PHP PCRE regex):
$re = '/\bSome text as:\s*\'\K[\w-]+/i';
$str = "Some text as: 'Nerad'";
if (preg_match($re, $str, $match)) {
echo $match[0];
} // => Nerad
See the PHP demo
Just come from the back and capture the word in a group. The Group 1, will have the required string.
/:\s*'(\w+)'$/
Dot (.) Meta character is not matching text and showing zero(o) output. Please tell me what i am missing in this code.
$string = 'pakistan';
echo preg_match('/p.n/',$string);
The following regex:
$string = 'pakistan';
echo preg_match('~^p.+n$~',$string);
Anchors the string to the beginning and end and requires at least one (but unlimited times) character between p and n.
Use below code:-
preg_match("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
If you want to count how many times specific word is repeating in the string
Use preg_match_all
preg_match_all ("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
Hope it will help you :)
I need to find the nearest name in string how would I do this ?
The closest I got was the apposite and it finds the furthest away from string is:
$string = "joe,bob,luis,sancho,bob,marco,lura,hannah,bob,marco,luis";
$new_string = preg_replace('/(bob(?!.*bob))/', 'found it!', $string);
echo $new_string;
<!-- outputs: joe,bob,luis,sancho,bob,marco,lura,hannah,found it!,marco,luis -->
How would I do the apposite ? and have an output like this:
<!-- outputs: joe,found it!,luis,sancho,bob,marco,lura,hannah,bob,marco,luis -->
The regex you are using (bob(?!.*bob)) matches the last occurrence of bob (not as a whole word) on a line, because the . matches any character but a newline, and the negative lookahead makes sure there is no bob after bob. See what your regex matches (if we use preg_replace with default options).
You may use
$re = '/\bbob\b/';
$str = "joe,bob,luis,sancho,bob,marco,lura,hannah,bob,marco,luis";
$result = preg_replace($re, 'found it!', $str, 1);
See IDEONE demo
The regex \bbob\b will match a whole word, and using the limit argument will only match the first occurrence of the word 'bob'.
See preg_replace help:
limit
The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).
You can try a negative lookbehind instead, like this
$string = "joe,bob,luis,sancho,bob,marco,lura,hannah,bob,marco,luis";
$new_string = preg_replace('/((?<!bob)bob)/', 'found it!', $string, 1);
echo $new_string;
<!-- outputs: joe,found it!,luis,sancho,bob,marco,lura,hannah,bob,marco,luisoff -->
As Wiktor said, use the limit option to match only the first occurrence of the name.
I am trying to make a regex that will look behind .txt and then behind the "-" and get the first digit .... in the example, it would be a 1.
$record_pattern = '/.txt.+/';
preg_match($record_pattern, $decklist, $record);
print_r($record);
.txt?n=chihoi%20%283-1%29
I want to write this as one expression but can only seem to do it as two. This is the first time working with regex's.
You can use this:
$record_pattern = '/\.txt.+-(\d)/';
Now, the first group contains what you want.
Your regex would be,
\.txt[^-]*-\K\d
You don't need for any groups. It just matches from the .txt and upto the literal -. Because of \K in our regex, it discards the previously matched characters. In our case it discards .txt?n=chihoi%20%283- string. Then it starts matching again the first digit which was just after to -
DEMO
Your PHP code would be,
<?php
$mystring = ".txt?n=chihoi%20%283-1%29";
$regex = '~\.txt[^-]*-\K\d~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 1
Hi how do I do a preg match on
$string1 = "[%refund%]processed_by"
$string2 = "[%refund%]date_sent"
I want to grab the bits inside %% and then remove the [%item%] altogether. leaving just the "proccessed_by" or "date_sent" I have had a go below but come a bit stuck.
$unprocessedString = "[%refund%]date_sent"
$match = preg_match('/^\[.+\]/', $unprocessedString);
$string = preg_replace('/^\[.+\]/', $unprocessedString);
echo $match; // this should output refund
echo $string; // this should output date_sent
Your problem is with your use of the preg_match function. It returns the number of matches found. But if you pass it a variable as a third parameter, it stores the matches for the entire pattern and its subpatterns in an array.
So you can capture both of the parts you want in subpatterns with preg_match, which means you don't need preg_replace:
$unprocessedString = "[%refund%]date_sent"
preg_match('/^\[%(.+)%\](.+)/', $unprocessedString, $matches);
echo $matches[1]; // outputs 'refund'
echo $matches[2]; // outputs 'date_sent'