使用 Int32 查找 Ctypes 的正则表达式

发布于 2024-10-08 19:45:23 字数 1673 浏览 4 评论 0原文

(嘿大家,

我正在寻找一些正则表达式帮助...

我正在尝试找到所有 CType(expression,Int32) 并将它们替换为 CInt(expression)

然而,这被证明是相当困难的,考虑到可能存在 , Int32) 有谁知道如何最好地执行此操作吗?

这就是我现在所拥有的:

Dim str As String = "CType((original.Width * CType((targetSize / CType(original.Height, Single)), Single)), Int32)"
Dim exp As New Regex("CType\((.+), Int32\)")
str = exp.Replace(str, "CInt($1)")

但这将匹配整个字符串并替换它。

在正则表达式匹配中嵌套 Ctype(表达式 递归函数找到最外层的匹配,然后向内工作,但这仍然存在诸如

CType(replaceChars(I), Int32)), Chr(CType(replacementChars(I), Int32)

任何提示输入输出之类的问题:编辑:一直在研究它,并有一个我喜欢

returnString.Replace(Chr(CType(replaceChars(I), Int32)), Chr(CType(replacementChars(I), Int32)))

递归

 returnString.Replace(Chr(CInt(replaceChars(I))),Chr(CInt(replacementChars(I))))

函数

。仍在解决问题。递归+正则表达式有点痛

 Private Function FindReplaceCInts(ByVal strAs As String) As String
    System.Console.WriteLine(String.Format("Testing : {0}", strAs))
    Dim exp As New Regex("CType\((.+), Int32\)")
    If exp.Match(strAs).Success Then

        For Each match As Match In exp.Matches(strAs)

            If exp.Match(match.Value.Substring(2)).Success Then
                Dim replaceT As String = match.Value.Substring(2)
                Dim Witht As String = FindReplaceCInts(match.Value.Substring(2))
                System.Console.WriteLine(strAs.IndexOf(replaceT))
                strAs.Replace(replaceT, Witht)
            End If

        Next

        strAs = exp.Replace(strAs, "CInt($1)")
    End If

    Return strAs
End Function

(Hey all,

I am looking for a little regex help...

I am trying to find all CType(expression,Int32) s and replace them with CInt(expression)

This, however, is proving quite difficult, considering there could be a nested Ctype(expression, Int32) within the regex match. Does anyone have any ideas for how to best go about doing this?

Here is what I have now:

Dim str As String = "CType((original.Width * CType((targetSize / CType(original.Height, Single)), Single)), Int32)"
Dim exp As New Regex("CType\((.+), Int32\)")
str = exp.Replace(str, "CInt($1)")

But this will match the entire string and replace it.

I was thinking of doing a recursive function to find the outer most match, and then work inwards, but that still presents a problem with things like

CType(replaceChars(I), Int32)), Chr(CType(replacementChars(I), Int32)

Any tips would be appreciated.

Input

returnString.Replace(Chr(CType(replaceChars(I), Int32)), Chr(CType(replacementChars(I), Int32)))

Output:

 returnString.Replace(Chr(CInt(replaceChars(I))),Chr(CInt(replacementChars(I))))

Edit:

Been working on it a little more and have a recursive function that I'm still working out the kinks in. Recursion + regex. it kinda hurts.

 Private Function FindReplaceCInts(ByVal strAs As String) As String
    System.Console.WriteLine(String.Format("Testing : {0}", strAs))
    Dim exp As New Regex("CType\((.+), Int32\)")
    If exp.Match(strAs).Success Then

        For Each match As Match In exp.Matches(strAs)

            If exp.Match(match.Value.Substring(2)).Success Then
                Dim replaceT As String = match.Value.Substring(2)
                Dim Witht As String = FindReplaceCInts(match.Value.Substring(2))
                System.Console.WriteLine(strAs.IndexOf(replaceT))
                strAs.Replace(replaceT, Witht)
            End If

        Next

        strAs = exp.Replace(strAs, "CInt($1)")
    End If

    Return strAs
End Function

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪都笑了 2024-10-15 19:45:24

尝试使用这个 (?!CType\(.+, )Int32 正则表达式而不是你的
您需要使用消极的前瞻来完成您的任务。

此网站检查正则表达式

try to use this (?!CType\(.+, )Int32 regex instead of yours
You need to use negative look ahead to accomplish your task.

Check regex at this site

断念 2024-10-15 19:45:24

我已经在 VS 2008 中尝试过此操作(没有 VS 2010 的副本来尝试),使用 Find &替换对话框:

正则表达式:CType\({.+}, Int32\)

替换为:CInt(\1)

它不会一次性修复嵌套情况,但您应该能够继续使用该模式进行搜索并替换,直到找不到其他匹配项。

顺便说一句:该对话框还提供了一个指向此帮助页面的链接,解释使用 VS 风格的正则表达式的字符 http://msdn.microsoft.com/en-us/library/aa293063(VS.71).aspx

I've tried this in VS 2008 (no copy of VS 2010 to try it out), using the Find & Replace dialog:

Regular Expression: CType\({.+}, Int32\)

Replace With: CInt(\1)

It won't fix the nested situations in one pass, but you should be able to continue searching with that pattern and replacing until no other matches are found.

BTW: That dialog also provides a link to this help page explaining characters used the VS flavor of regex http://msdn.microsoft.com/en-us/library/aa293063(VS.71).aspx

九命猫 2024-10-15 19:45:23

大家对此有何看法?

我认为它对于我迄今为止测试过的各种情况都做得很好......

Private Function FindReplaceCInts(ByVal strAs As String) As String
    Dim exp As New Regex("CType\((.+), Int32\)")
    If exp.Match(strAs).Success Then

        For Each match As Match In exp.Matches(strAs)

            If exp.Match(match.Value.Substring(2)).Success Then
                Dim replaceT As String = match.Value.Substring(2)
                Dim Witht As String = FindReplaceCInts(match.Value.Substring(2))
                strAs = strAs.Replace(replaceT, Witht)
            End If

        Next

        strAs = exp.Replace(strAs, "CInt($1)")
    End If

    Return strAs
End Function

What do you guys think of this?

I think it does it quite nicely for a variety of cases that I have tested so far...

Private Function FindReplaceCInts(ByVal strAs As String) As String
    Dim exp As New Regex("CType\((.+), Int32\)")
    If exp.Match(strAs).Success Then

        For Each match As Match In exp.Matches(strAs)

            If exp.Match(match.Value.Substring(2)).Success Then
                Dim replaceT As String = match.Value.Substring(2)
                Dim Witht As String = FindReplaceCInts(match.Value.Substring(2))
                strAs = strAs.Replace(replaceT, Witht)
            End If

        Next

        strAs = exp.Replace(strAs, "CInt($1)")
    End If

    Return strAs
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文