【问题标题】:Convert C# regex Code to Java将 C# 正则表达式代码转换为 Java
【发布时间】:2011-07-02 21:40:49
【问题描述】:

我在 C# 中找到了这个正则表达式提取器代码。 谁能告诉我这是如何工作的,以及如何用 Java 编写等价物?

// extract songtitle from metadata header. 
// Trim was needed, because some stations don't trim the songtitle
fileName = 
    Regex.Match(metadataHeader, 
      "(StreamTitle=')(.*)(';StreamUrl)").Groups[2].Value.Trim();

【问题讨论】:

    标签: c# java regex


    【解决方案1】:

    这应该是你想要的。

    // Create the Regex pattern
    Pattern p = Pattern.compile("(StreamTitle=')(.*)(';StreamUrl)");
    // Create a matcher that matches the pattern against your input
    Matcher m = p.matcher(metadataHeader);
    // if we found a match
    if (m.find()) {
        // the filename is the second group. (The `(.*)` part)
        filename = m.group(2);
    }
    

    【讨论】:

      【解决方案2】:

      它从诸如“StreamTitle='MyTitle';StreamUrl”之类的字符串中提取“MyTitle”。

      () 运算符定义匹配组,您的正则表达式中有 3 个。第二个包含感兴趣的字符串,并在 Groups[2].Value 中获取。

      那里有一些非常优秀的正则表达式设计器。我使用的是 Rad Software 的正则表达式设计器 (www.radsoftware.com.au)。它对于找出这样的东西非常有用(并且它使用 C# RegEx)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        相关资源
        最近更新 更多