简体   繁体   English

正则表达式以匹配模式

[英]Regular Expression to match the pattern

I am looking for Regular Expression search pattern to find data within $< and >$ . 我正在寻找正则表达式搜索模式以查找$<>$内的数据。

string pattern = "\b\$<[^>]*>\$"; 

is not working. 不管用。

Thanks, 谢谢,

You can make use of a tempered greedy token : 您可以使用脾气暴躁的令牌

\$<(?:(?!\$<|>\$)[\s\S])*>\$

See demo 观看演示

This way, you will match only the closest boundaries. 这样,您将仅匹配最接近的边界。

Your regex does not match because you do not allow > in-between your markers, and you are using \\b where you most probably do not have a word boundary. 您的正则表达式不匹配,因为您不允许在标记之间使用> ,并且您正在使用\\b ,而您最可能没有单词边界。

If you do not want to get the delimiters in the output, use capturing group: 如果不想在输出中获取定界符,请使用捕获组:

\$<((?:(?!\$<|>\$)[\s\S])*)>\$
   ^                      ^

And the result will be in Group 1 . 结果将在第1组中

In C#, you should consider declaring all regex patterns (whenever possible) with the help of a verbatim string literal notation (with @"" ) because you won't have to worry about doubling backslashes: 在C#中,您应该考虑借助逐字字符串文字符号(带有@"" )声明所有正则表达式模式(如果可能),因为您不必担心加倍反斜杠:

var rx = new Regex(@"\$<(?:(?!\$<|>\$)[\s\S])*>\$");

Or, since there is a singleline flag (and this is preferable): 或者,由于存在单行标记(这是更好的选择):

var rx = new Regex(@"\$<((?:(?!\$<|>\$).)*)>\$", RegexOptions.Singleline | RegexOptions.CultureInvariant);
var res = rx.Match(text).Select(p => p.Groups[1].Value).ToList();

This pattern will do the work: 此模式将完成工作:

(?<=\$<).*(?=>\$)

Demo: https://regex101.com/r/oY6mO2/1 演示: https//regex101.com/r/oY6mO2/1

To find this pattern in php you have this REGEX code for find any patten, 要在php中找到此模式,您可以使用此REGEX代码来查找任何模式,

/$<(.*?)>$/s /$<(.*?)>$/s

For Example: 例如:

        $arrayWhichStoreKeyValueArrayOfYourPattern= array();
        preg_match_all('/$<(.*?)>$/s', 
        $yourcontentinwhichyoufind,         
        $arrayWhichStoreKeyValueArrayOfYourPattern);
        for($i=0;$i<count($arrayWhichStoreKeyValueArrayOfYourPattern[0]);$i++)
        {
            $content=
                     str_replace(
                      $arrayWhichStoreKeyValueArrayOfYourPattern[0][$i], 
                      constant($arrayWhichStoreKeyValueArrayOfYourPattern[1][$i]), 
                      $yourcontentinwhichyoufind);
        }

using this example you will replace value using same name constant content in this var $yourcontentinwhichyoufind 使用此示例,您将在此变量$ yourcontentin中找到使用相同名称常量内容替换的值

For example you have string like this which has also same named constant. 例如,您具有类似这样的字符串,它也具有相同的命名常量。

**global.php**
//in this file my constant declared.

define("MYNAME","Hiren Raiyani");
define("CONSTANT_VAL","contant value");

**demo.php**
$content="Hello this is $<MYNAME>$ and this is simple demo to replace $<CONSTANT_VAL>$";
$myarr= array();
        preg_match_all('/$<(.*?)>$/s', $content,      $myarray);
        for($i=0;$i<count($myarray[0]);$i++)
        {
            $content=str_replace(
                      $myarray[0][$i], 
                      constant($myarray[1][$i]), 
                      $content);
        }

I think as i know that's all. 我想我知道就这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM