1

Given the following text of example:

Hello $NAME$ how are you? You are visiting this $LOGIN_PAGE$ page, please fill the form and log in.

I want to select only the following "variable" $LOGIN_PAGE$ without select even the $NAME character.

I made this regex trying to achieve the result but it select both of the string

(\$([A-Z\_])+\$)

Which part I'm missing in this regex?

4
  • 1
    why don't use (\$LOGIN_PAGE\$)? Commented Jul 19, 2014 at 16:41
  • str_replace('$LOGIN_PAGE$', '/login', $haystack) maybe? Commented Jul 19, 2014 at 16:42
  • prefer regex for more special strings Commented Jul 19, 2014 at 16:45
  • Sorry not clear if the string is constant then why are you using regex. Commented Jul 19, 2014 at 16:45

1 Answer 1

1

The below regex would match only the the string which has only capital letters separated by underscore symbol where the start and end of the string is $,

\$[A-Z]+_[A-Z]+\$

DEMO

Your PHP code would be,

<?php
$data = 'Hello $NAME$ how are you? You are visiting this $LOGIN_PAGE$ page, please fill the form and log in.' ;
$regex =  '~\$[A-Z]+_[A-Z]+\$~';
if (preg_match($regex, $data, $m)) {
    $yourmatch = $m[0]; 
    echo $yourmatch;
    }
?> //=> $LOGIN_PAGE$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.