字符串内容
abc def ghi jkl mno 
{abc def ghi jkl mno} 
abc def ghi jkl mno 
{abc def ghi jkl mno}用正规表达式怎样把{}之外内容中的def替换成opq,jkl替换成mno?查找{}之间内容的表达式可以用"(\{)(\r\n)*(.*)(\})"

解决方案 »

  1.   

    char seps[]   = "{}";
    char *token;
    /* Get the first token: */
    token = strtok( strLsh, seps );
    {
                             }
    /* Get next token: */
    token = strtok( NULL, seps );
      

  2.   

    不明白你的意思
    正规表达式好像是用来定义文法的,不是用来替换什么东西的。你是想用Lex作吗?拜托你说清楚点
      

  3.   

    可以用来替换的,比如PHP中如下
    $bbcode = preg_replace("/(\[)({)(])(\r\n)*(.*)(\[\/}\])/esiU", "\\2", $bbcode);那我再说一次我的要求
    字符串内容:
    abc def ghi jkl mno 
    {abc def ghi jkl mno} 
    abc def ghi jkl mno 
    {abc def ghi jkl mno}用正规表达式怎样把{}之外内容中的def替换成opq,jkl替换成mno?
    替换的结果如下:
    abc opq ghi mno mno 
    {abc def ghi jkl mno} 
    abc opq ghi mno mno 
    {abc def ghi jkl mno}
      

  4.   

    under linux/unix
    ^[^{]def^[$}]/opq
      

  5.   

    under linux/unix
    ^[^{]def^[$}]/opq我试了,不行。
      

  6.   

    <?php
    $s = "abc def ghi jkl mno 
    {abc def ghi jkl mno} 
    abc def ghi jkl mno 
    {abc def ghi jkl mno}
    ";
    $patterns = array(
    "/([^{]*)def([^}]*)/",
    "/([^{]*)jkl([^}]*)/"
    );
    $replace = array(
    "\\1opg\\2",
    "\\1mno\\2"
    );
    echo preg_replace($patterns,$replace,$s);
    ?>
      

  7.   

    try the following in IE6, although it may not work with IE5, also the regex makes no other assumptions, so if you have other kind of strings, the following may not work<script>
    function rep(s)
    {
      if (s.toLowerCase() == "def")
        return "opq";
      else if (s.toLowerCase() == "jkl") 
        return "mno";
      else
        return s;
    }s = "abc def ghi jkl mno \n{abc def ghi jkl mno} \nabc def ghi jkl mno \n{abc def ghi jkl mno}";
    re = /\b(def|jkl)\b(?=[^{}]+\{)/gmi;
    s = s.replace(re, function(){ return rep(arguments[0]);});
    alert(s);
    </script>
      

  8.   

    试了一下xuzuning(唠叨) 兄弟的,果然可以
    <?php
    $s = "abc def ghi jkl mno 
    {abc def ghi jkl mno} 
    abc def ghi jkl mno 
    {abc def ghi jkl mno}
    ";
    $patterns = array(
    "/([^{]*)def([^}]*)/",
    "/([^{]*)jkl([^}]*)/"
    );
    $replace = array(
    "\\1opg\\2",
    "\\1mno\\2"
    );
    echo preg_replace($patterns,$replace,$s);
    ?>要是分隔符{变成[code],}变成[/code],那应该怎样变呢?
      

  9.   

    正则表达式vc里面有吗?
    我只在perl 和 php里面学过
      

  10.   

    没有人回答
    $searcharray  = array("/([^\x80]*)asdf([^\x81]*)/i");
    $replacearray = array("\\1凌萍\\2");怎样替换全部