【问题标题】:Need Help Converting Regex From VB.NET To C# [closed]需要帮助将正则表达式从 VB.NET 转换为 C# [关闭]
【发布时间】:2011-12-24 18:19:09
【问题描述】:

我聘请了一名编码员用 Regex 对我进行编码。问题是他是用 Visual Basic 编写的,而我需要它是用 C# 编写的。

我尝试使用转换器,但没有解决问题。

介意帮我将以下正则表达式转换为 C# 吗?问题在于 GetBetween 和 Strings 谢谢:

string iamtwit = ss("http://www.mailinator.com" + GetBetween(GetBetween(iamtwit1, matches.ToString(), "</a>"), "<a href=", ">"));

MessageBox.Show(GetBetween(matches1.ToString(), "<a href=\"", Strings.Chr(34)));

这里是原始代码:

Dim iamtwit As String = ss("http://www.mailinator.com" & GetBetween(GetBetween(iamtwit1, matches.ToString, "</a>"), "<a href=", ">"))


MsgBox(GetBetween(matches1.ToString, "<a href=""", Chr(34)))

我收到的错误消息是:

当前上下文中不存在名称“GetBetween”

当前上下文中不存在名称“字符串”

【问题讨论】:

  • 那是 C#...注意分号?请发布 VB.NET 代码,以便我们准确了解发生了什么。
  • 但我收到一条错误消息:GetBetween
  • GetBetween 是一个函数...可能还需要从VB.NET代码转换过来
  • @DanAndrews - 为什么需要转换?它必须在引用的程序集中才能工作。
  • @Oded,如果它在他试图转换的类中。 Ash 也是对的。

标签: c# regex vb.net


【解决方案1】:

GetBetween 很可能是来自this article 的代码。

转换为 C#:

    public string GetBetween(string sSearch, string sStart, string sStop, int lSearch = 1)
    {
        string retVal = null;
        lSearch = (sSearch.IndexOf(sStart, lSearch - 1, StringComparison.InvariantCultureIgnoreCase) + 1);
        if (lSearch > 0)
        {
            lSearch = lSearch + sStart.Length;
            int lTemp = 0;
            lTemp = (sSearch.IndexOf(sStop, lSearch - 1) + 1);
            if (lTemp > lSearch)
            {
                retVal = sSearch.Substring(lSearch - 1, lTemp - lSearch);
            }
        }
        return retVal;
    }

【讨论】:

    【解决方案2】:
    private string getBetween(string strSource, string strStart, string strEnd)
            {
                int Start, End;
                if (strSource.Contains(Starting) && strSource.Contains(Ending))
                {
                    Start = strSource.IndexOf(Starting, 0) + strStart.Length;
                    End = strSource.IndexOf(strEnd, Start);
                    return strSource.Substring(Start, End - Start);
                }
                else
                {
                    return "";
                }
            }
    

    【讨论】:

    • Contains 只是 IndexOf 函数的包装。您应该直接调用 IndexOf 并检查结果是否为 -1
    • 我同意不使用Contains。然而,它不是为了只搜索一次字符串的(可能是微的)优化,而是当前实现在调用getBetween("&lt;/a&gt;&lt;a&gt;test", "&lt;a&gt;", "&lt;/a&gt;") 时抛出。此外,您正在使用 StartingEnding 而不定义它们。
    【解决方案3】:

    您在 C# 中发布的代码,而不是 VB.NET。它也不包含任何正则表达式。

    导入程序集并直接使用类型,您似乎已经这样做了。我假设你需要GetBetween 方法。

    .NET 的一个特点是人们可以使用不同的语言并与它们进行互操作。

    【讨论】:

    • 如何解决 Getbetween 问题?
    • @user1017524 - 你实际上并没有解释问题是什么
    猜你喜欢
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2023-03-19
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多