PCRE regex to sed regex

前端 未结 6 1039
眼角桃花
眼角桃花 2021-01-22 05:32

First of all sorry for my bad english. I\'m a german guy.

The code given below is working fine in PHP:

$string = preg_replace(\'/href=\"(.*?)(\\.|\\,)\"/         


        
相关标签:
6条回答
  • 2021-01-22 06:14

    You need a backslash in front of the parentheses you want to reference, thus

    sed 's/href="\(.*?\)(.|\,)"/href="{$\1}"/g' test.htm
    
    0 讨论(0)
  • 2021-01-22 06:15
    sed -e 's|href=\"\(.[^"][^>]*\)\([.,]\)\">|href="\1">|g' file
    
    0 讨论(0)
  • 2021-01-22 06:23

    sed does not support non-greedy regex match.

    0 讨论(0)
  • 2021-01-22 06:26

    here is a solution, it is not prefect, only deal with the situation of one extra "," or "."

    
    sed -r -e 's/href="([^"]*)([.,]+)"/href="\1"/g' test.htm
    
    0 讨论(0)
  • 2021-01-22 06:34

    You have to escape the block selector characters ( and ) as follows.

    sed 's/href="\(.*?\)\(.|\,\)"/href="{$\1}"/g' test.htm
    
    0 讨论(0)
  • 2021-01-22 06:38

    If you want to match a literal ".", you need to escape it or use it in a character class. As an alternative to slashing the capturing parentheses (which you need to do with basic REs), you can use the -E option to tell sed to use extended REs. Lastly, the REs used by sed use \N to refer to subpatterns, where N is a digit.

    sed -E "s/href=([\"'])([^\"']*)[.,]\1/href=\1\2\1/i"
    

    This has its own issue that will prevent matches of href attributes that use both types of quotes.

    man sed and man re_format will give more information on REs as used in sed.

    0 讨论(0)
提交回复
热议问题