Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何替换包含#_Regex_R_String_Text - Fatal编程技术网

Regex 如何替换包含#

Regex 如何替换包含#,regex,r,string,text,Regex,R,String,Text,在我的数据集中有一个变量,其值包含html代码,例如: 这些都不是 我想用其他替换为: df$Country <- gsub("<font color="#800080">None of these</font>", "Other", df$Country) 有两种选择。两者都假设我们从这样的东西开始 x <- '<font color="#800080">None of these</font>' 选项2:转义引号字符。这很简单

在我的数据集中有一个变量,其值包含html代码,例如:
这些都不是

我想用其他替换为:

df$Country <- gsub("<font color="#800080">None of these</font>", "Other", df$Country)

有两种选择。两者都假设我们从这样的东西开始

x <- '<font color="#800080">None of these</font>'
  • 选项2:转义引号字符。这很简单,只需在引号前加一个
    \
    ,表示应该转义

    gsub("<font color=\"#800080\">None of these</font>", "other", x)
    
    gsub(“没有这些”、“其他”,x)
    

  • 有两种选择。两者都假设我们从这样的东西开始

    x <- '<font color="#800080">None of these</font>'
    
  • 选项2:转义引号字符。这很简单,只需在引号前加一个
    \
    ,表示应该转义

    gsub("<font color=\"#800080\">None of these</font>", "other", x)
    
    gsub(“没有这些”、“其他”,x)
    

  • 有两种选择。两者都假设我们从这样的东西开始

    x <- '<font color="#800080">None of these</font>'
    
  • 选项2:转义引号字符。这很简单,只需在引号前加一个
    \
    ,表示应该转义

    gsub("<font color=\"#800080\">None of these</font>", "other", x)
    
    gsub(“没有这些”、“其他”,x)
    

  • 有两种选择。两者都假设我们从这样的东西开始

    x <- '<font color="#800080">None of these</font>'
    
  • 选项2:转义引号字符。这很简单,只需在引号前加一个
    \
    ,表示应该转义

    gsub("<font color=\"#800080\">None of these</font>", "other", x)
    
    gsub(“没有这些”、“其他”,x)
    

  • html上正则表达式的所有这些问题都是不使用它的原因。假设您的数据最初是一个实际的html文档,请改用XPath。下面是一个例子:

    html.text <- '<html>
    <head></head>
    <body>
    <div><font color="#800080">None of these</font></div>
    </body>
    <html>'
    
    library(XML)
    html <- htmlTreeParse(html.text,useInternalNodes=TRUE)
    replaceNodes(html['//font[@color="#800080"]'][[1]],"Other")
    # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # <html>
    # <head></head>
    # <body>
    # <div>Other</div>
    # </body>
    # </html>
    

    html.texthtml上的正则表达式的所有这些问题都是不使用它的原因。假设您的数据最初是一个实际的html文档,请改用XPath。下面是一个例子:

    html.text <- '<html>
    <head></head>
    <body>
    <div><font color="#800080">None of these</font></div>
    </body>
    <html>'
    
    library(XML)
    html <- htmlTreeParse(html.text,useInternalNodes=TRUE)
    replaceNodes(html['//font[@color="#800080"]'][[1]],"Other")
    # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # <html>
    # <head></head>
    # <body>
    # <div>Other</div>
    # </body>
    # </html>
    

    html.texthtml上的正则表达式的所有这些问题都是不使用它的原因。假设您的数据最初是一个实际的html文档,请改用XPath。下面是一个例子:

    html.text <- '<html>
    <head></head>
    <body>
    <div><font color="#800080">None of these</font></div>
    </body>
    <html>'
    
    library(XML)
    html <- htmlTreeParse(html.text,useInternalNodes=TRUE)
    replaceNodes(html['//font[@color="#800080"]'][[1]],"Other")
    # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # <html>
    # <head></head>
    # <body>
    # <div>Other</div>
    # </body>
    # </html>
    

    html.texthtml上的正则表达式的所有这些问题都是不使用它的原因。假设您的数据最初是一个实际的html文档,请改用XPath。下面是一个例子:

    html.text <- '<html>
    <head></head>
    <body>
    <div><font color="#800080">None of these</font></div>
    </body>
    <html>'
    
    library(XML)
    html <- htmlTreeParse(html.text,useInternalNodes=TRUE)
    replaceNodes(html['//font[@color="#800080"]'][[1]],"Other")
    # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # <html>
    # <head></head>
    # <body>
    # <div>Other</div>
    # </body>
    # </html>
    


    html.text您能给出df的示例输出吗?此外,字符串中还有双引号。你需要逃避这些。在会话中读取时,默认情况下应该转义。如果要替换的字符串需要匹配特定字符串(而不是模式),请在调用
    gsub
    时使用
    fixed=TRUE
    参数,是否可以发布
    dput
    。当然,正如其他人所建议的,您还需要转义引号。一个选项是转义,另一个选项是在模式周围使用单引号进行匹配。演示:
    x我已经包括了一个
    dput
    。你能给出df的输出示例吗?此外,字符串中还有双引号。你需要逃避这些。在会话中读取时,默认情况下应该转义。如果要替换的字符串需要匹配特定字符串(而不是模式),请在调用
    gsub
    时使用
    fixed=TRUE
    参数,是否可以发布
    dput
    。当然,正如其他人所建议的,您还需要转义引号。一个选项是转义,另一个选项是在模式周围使用单引号进行匹配。演示:
    x我已经包括了一个
    dput
    。你能给出df的输出示例吗?此外,字符串中还有双引号。你需要逃避这些。在会话中读取时,默认情况下应该转义。如果要替换的字符串需要匹配特定字符串(而不是模式),请在调用
    gsub
    时使用
    fixed=TRUE
    参数,是否可以发布
    dput
    。当然,正如其他人所建议的,您还需要转义引号。一个选项是转义,另一个选项是在模式周围使用单引号进行匹配。演示:
    x我已经包括了一个
    dput
    。你能给出df的输出示例吗?此外,字符串中还有双引号。你需要逃避这些。在会话中读取时,默认情况下应该转义。如果要替换的字符串需要匹配特定字符串(而不是模式),请在调用
    gsub
    时使用
    fixed=TRUE
    参数,是否可以发布
    dput
    。当然,正如其他人所建议的,您还需要转义引号。一个选项是转义,另一个选项是在模式周围使用单引号进行匹配。演示:
    x我包含了一个
    dput
    。数据来自SPSS文件,这个
    html
    代码包含在一些变量的值中+1但是为了得到一个好的答案,我可能需要它来解决另一个问题:-)数据来自SPSS文件,这个
    html
    code包含在一些变量的值中+1但是为了得到一个好的答案,我可能需要它来解决另一个问题:-)数据来自SPSS文件,这个
    html
    code包含在一些变量的值中+1但是为了得到一个好的答案,我可能需要它来解决另一个问题:-)数据来自SPSS文件,这个
    html
    code包含在一些变量的值中+1尽管答案很好,但我可能需要它来解决另一个我必须解决的问题:-)