惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
T
Tailwind CSS Blog
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Heimdal Security Blog
博客园 - 【当耐特】
W
WeLiveSecurity
J
Java Code Geeks
Latest news
Latest news
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
博客园 - Franky
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
PCI Perspectives
PCI Perspectives
博客园_首页
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
P
Palo Alto Networks Blog
I
InfoQ
Security Latest
Security Latest
Hacker News: Ask HN
Hacker News: Ask HN
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
F
Full Disclosure
Cyberwarzone
Cyberwarzone
D
DataBreaches.Net
The Cloudflare Blog
S
Securelist
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
AI
AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

博客园 - 吾非无心

MSB4019 找不到导入的项目“C:\Users\...\AppData\Local\QtMsBuild\vs-debugtools\qt_import.props” - 吾非无心 - 博客园 类/结构最后一个成员为类(string)时可能会出现“堆损坏”(HEAP CORRUPTION DETECTED)错误 GCC在C语言中内嵌汇编 asm __volatile__ double转int QJson 存储(u)longlong有问题 QT QPixmap QImage内存泄漏 QMetaObject::connectSlotsByName: No matching signal for QT数据库连接管理类 QT多重继承带来的问题及解决办法,记录备查 QT6 源码编译Win32 x86 vc++高精度计时sleep stack smashing detected 莫名其妙的错误 strdump的问题 再加一个realloc的问题 linux下简单封装读写锁 codeblocks下libacl配置 centos8 安装、配置redis6 pyftpdlib中文乱码的解决之道 CentOS8让uwsgi开机自动启动django(无需登录,无需手动) libevent学习一
QT删除python中的单行注释
吾非无心 · 2023-11-05 · via 博客园 - 吾非无心

python中的#号可能存在于字符串中:

print("'asd#f'00#0" , '#1“23')  #这里才开始注释

没想到用什么正则来删除python中的单行注释

所以解决方案为:

QString removeLineComment(QString sLine)
{
    qDebug() << "removeLineComment:" << sLine;
    QString sRes;
    int nSharpPos = sLine.indexOf('#');
    if (nSharpPos < 0)
        return sLine;
    if (nSharpPos == 0) {
        return "";
    }
    if (sLine.indexOf('\'') < 0 && sLine.indexOf('\"') < 0) {//该行无引号
        sRes = sLine.left(nSharpPos);
        return sRes;
    }

    struct _s_e_ {
        int start;
        int end;
    };
    QList<_s_e_> _list;
    QRegularExpression regex_str("(['\"]{1}).*?\\1");
    auto matches_str = regex_str.globalMatch(sLine);
    if (matches_str.isValid()) {
        while (matches_str.hasNext()) {
            auto m=matches_str.next();
            if (m.hasMatch()) {
                _s_e_ se;
                se.start = m.capturedStart();
                se.end = m.capturedEnd();
                _list << se;                
            }
        }
    }

    sRes = sLine;
    while (nSharpPos>0) {
        bool bInStr = false;
        for (auto se : _list) {
            bInStr = nSharpPos > se.start&&nSharpPos < se.end;
            if (bInStr)
                break;
        }
        if (!bInStr) {
            sRes = sLine.left(nSharpPos);
            break;
        }
        nSharpPos = sLine.indexOf('#', nSharpPos+1);
    }


    return sRes;
}

View Code