Skip to content

bugfix:正则表达式字符转义 - #1

Merged
DebugST merged 1 commit into
DebugST:mainfrom
VisualLabelDesigner:main
Mar 3, 2023
Merged

bugfix:正则表达式字符转义#1
DebugST merged 1 commit into
DebugST:mainfrom
VisualLabelDesigner:main

Conversation

@VisualLabelDesigner

Copy link
Copy Markdown

当选中编辑器里的字符时,需对正则表达式的保留字符转义

@DebugST

DebugST commented Mar 2, 2023

Copy link
Copy Markdown
Owner

一开始的时候我也思考过这个问题。但是后来想了想,其实不存在这样的情况。因为你仔细查看代码。

public void OnSelectionChanged(TextManager textManager, int nStart, int nLen) {
    // ... other code ...
    var line = textManager.GetLineFromCharIndex(nStart);
    // m_spliter -> 单词拆分器 确保m_spliter.Each()中每一个都是完整的单词。当然如果m_spliter没有bug的话
    m_spliter.Each(line.RawString, nStart - line.IndexOfFirstChar, (str, ns, nl) => {
        // ns -> Index of word Start
        // nl -> Length of word
        if (ns + line.IndexOfFirstChar == nStart && nLen == nl) {
            strKey = str.Substring(ns, nl);
        }
        // 所以上面的判断是 
        // 如果当前选中的起始索引和它起始行所在的某个单词的索引一致
        // 并且
        // 选中的长度和单词长度一致
        // strKey 才是有效的 所以strKey中间不会出现任何正则相关的字符 因为那将变成多个单词
        return false;
    });
    // ... other code ...
}

如果你有一下文本

[START]
abTESTcd
(TEST)
(TE.T)
.TEST.
[END]

如果你选中TE.T或者第一行中的TEST你会发现其他的并不会高亮

@VisualLabelDesigner

Copy link
Copy Markdown
Author

你说的有道理。
我描述的问题是当选中()等正则保留字符时,即strKey值为(),会抛出一个异常。因为"\\b" + strKey + "\\b"此时的值为@"\b(\b"@"\b)\b"
你看看怎么处理比较好?

@DebugST
DebugST merged commit 316c51a into DebugST:main Mar 3, 2023
@DebugST

DebugST commented Mar 3, 2023

Copy link
Copy Markdown
Owner

确实会有你提到的问题。如果是选择单词没有任何问题。。如果选择的是一些或者一个符号就可能会出现bug。因为单独的或者同系列的符号也会被spliter拆分成单词。但是你的代码同样也有bug。我将合并后重新修改。
如果有一下文本

[START]
((
[END]

spliter会把((当作一个单词,,而你的代码执行后正则会变成\b\((\b。当然这是一个错位的正则。
我将添加如下代码:

private Regex m_reg_reg = new Regex(@"([*.?+$^\[\](){}|\\])");
//...
strKey = m_reg_reg.Replace(strKey, @"\$1");

DebugST added a commit that referenced this pull request Mar 3, 2023
DebugST added a commit that referenced this pull request Mar 3, 2023
DebugST added a commit that referenced this pull request Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants