首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Regex用反斜杠替换斜杠#包括双引号

Regex用反斜杠替换斜杠#包括双引号
EN

Stack Overflow用户
提问于 2014-05-31 09:28:02
回答 1查看 816关注 0票数 1

我想用反斜杠代替斜杠里面的#包括双引号,像这样。

先于

代码语言:javascript
复制
#include "Monster.h"
#include "ItemManager.h"
#include "Skil/Server/Manager/Skillmanager.h"
#include "Quest/QuestEvent.h"
#include "AES/AES.h"
#include "Trigger/Timer.h"

// Timer Class
CTimer::CTimer()
{
    printf("Minute / Second");
    printf("\t%2d:%2d\n", tMinute, tSecond);
}

代码语言:javascript
复制
#include "Monster.h"
#include "ItemManager.h"
#include "Skil\Server\Manager\Skillmanager.h"
#include "Quest\QuestEvent.h"
#include "AES\AES.h"
#include "Trigger\Timer.h"

// Timer Class
CTimer::CTimer()
{
    printf("Minute / Second");
    printf("\t%2d:%2d\n", tMinute, tSecond);
}

我希望只在#include标记中替换斜杠,而不是替换printf标记和注释标记中的斜杠。

我用来搜索的正则表达式是什么?

谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-31 11:32:09

您的代码在C++中,但您还没有说明如何替换斜杠:这可能是在IDE中、使用脚本或使用其他工具。

下面是使用regex模式下的搜索和替换来工作Notepad++ (测试)的解决方案。

搜索:(?m)(?:^#include\s*"|\G)[^/"]*\K/

替换:\

解释:

代码语言:javascript
复制
(?m)(?:^#include\s*"|\G)[^/"]*\K/
  • 将这些选项用于整个正则表达式(?m)
    • ^$ & m在换行处的匹配

  • 匹配(?:^#include\s*"|\G) 下面的正则表达式
    • 匹配此选项(只有在此选项失败时才尝试下一个选项) ^#include\s*"
      • 在行的开头(字符串的开头或断行字符之后)(行提要)断言位置( ^ )
      • 匹配字符串“#include”字面上(区分大小写的) #include
      • 匹配单个字符,即“空格字符”(ASCII空格、选项卡、行提要、回车、垂直选项卡、表单提要) \s*
        • 在零次和无限次之间,尽可能多次地返回(贪婪的) *

代码语言:javascript
复制
    - Match the character “"” literally `"`

代码语言:javascript
复制
- Or match this alternative (the entire group fails if this one fails to match) `\G` 
    - Assert position at the end of the previous match (the start of the string for the first attempt) `\G`

  • 匹配列表“/”[^/"]* 中不存在的任何单个字符
    • 在零次和无限次之间,尽可能多次地返回(贪婪的) *

  • 将文本与整体regex匹配的\K保持很远的匹配
  • 匹配字符“/”字面上的/
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23968429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档