preg_match pattern with signal character [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need a pattern which is able to find a word wrapped in two signal characters.
Basically something like
$string = "bablabla __test__ blablabla"
preg_match("/__\w__/", $string, $result);
print_r($result);

\w is a single word character. It would match __t__ but not multiple characters like __test__. Try \w+

Related

REGEX for alternate characters in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string mystring. I have to replace the alternate characters with x so that final output is mxsxrxnx. All the alternate characters starting from 2nd position are replaced with x. I can do it with loop but is there a Regular expression for that or a better way in PHP? Please help.
Using a reset match meta-character \K you are able to do it:
.\K.
Live demo
PHP:
echo preg_replace('/.\K./', 'x', 'mystring'); // mxsxrxnx

preg match extract digits from string after pattern [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string of the form
<something>_string_t_<digits>
How can I extract just the <digits> portion in PHP ? I need to detect the string_t first and then pull out the digits.
You want something like this:
$captures;
preg_match('/string_t_(\d+)/', $string, $captures);
do_something($captures[1]);
$captures[1] will contain <digits> (assuming <digits> is composed entirely of numbers - if it's not, use the more general (.+) in place of (\d+)).

Find the last hyperlink in a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to find the last hyperlink in a string. The hyperlink may begin with one of the following:
http://
https://
market://
Is there a regex method in PHP to find this?
Thank you.
You can use this negative lookahead based regex:
~\b(?:https?|market)://\S+?(?!.*?\b(?:https?|market)://)~i
$regex = "/(http|https|market)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "http://link.com some text in between https://link.com more text in between market://link.com";
if(preg_match_all($regex, $text, $url))
{
echo $url[0][count($url[0])-1]; // Outputs: market://link.com
}

Regex Replace pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need the regular expression to replace using following rule:
Any integer present before . then no take place.
If no integer present before . then that should be replace.
$input = (contain anything characters, symbols, numbers, floats etc)
Example:
$myString = "Example 1.58 Stack.68";
Output should be
Example 1.58 Stack,68
preg_replace('#(?<!\d)\.#',',',$myString)

Regex improvement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have the following regex:
/%%[\s\S]*?%%/i
It's meant to catch these types of strings:
%%test%%
%%test2%%
However, because this regex runs on big strings, sometimes there are mistakes, for example if i have this:
%test%% adhja kshdjah skdja %%test1%% it will return %% adhja kshdjah skdja %%
The strings that it is supposed to catch never have any spaces in them, how can I alter my regex to take only the ones with no spaces?
Use [^%\s] for non space and non %.
/%%[^%\s]*?%%/

Categories