用于提取公民号码的正则表达式公式

Regex formula to extract civic numbers

我有这个正则表达式公式,可以使用 SSIS 和脚本组件从地址列表中提取公民号码。

public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        //Replace each \ with \ so that C# doesn't treat \ as escape character
        //Pattern: Start of string, any integers, 0 or 1 letter, end of word
        string sPattern = "^[0-9]+([A-Za-z]\b)?";
        string sString = Row.ADDRESS ?? ""; //Coalesce to empty string if NULL

        //Find any matches of the pattern in the string
        Match match = Regex.Match(sString, sPattern, RegexOptions.IgnoreCase);
        //If a match is found
        if (match.Success)
            //Return the first match into the new
            //HouseNumber field
            Row.CivicNumber = match.Groups[0].Value;
        else
            //If not found, leave the HouseNumber blank
            Row.CivicNumber = "";
    }

}

In 适用于

这样的地址

我确实有一些这种格式的 :

我如何修改我的 Regex 公式以 return 我想要的结果?

以数字开头直到 space 的任何内容怎么样?例如:

\d\S+

如果 address/line 必须以数字开头,那么您可以使用上面问题中的锚点:

^\d\S+

信誉不足无法发表评论

所以根据你的两个例子,我可以假设第一个 space 之前的子字符串是你的公民号码吗? 如果是,那么您可以将单词拆分为 space 个字符,并从字符串数组中获取第一个子字符串。

String address = "49b/15 Main Street";
String[] addressArr = address.split("\s+");
System.out.println(addressArr[0]);

试试这个:

string sPattern = "^[0-9]+[A-Za-z]?\b(/[0-9]+)?";