我有以下数据:
123ST
89ST
101ST
1ST
TEST
我想区分 first 和 street,然后在最后一个字符不包含 1 的情况下放置一个空格。
Add Space:
123 ST
89 ST
Do not modify:
101ST
1ST
TEST
这是我目前所知道的,但还没有找到解决方案:
address = Regex.Replace(address, @"\sST", " ");
注意:不应像测试示例中看到的那样修改非数字数据。
答案 0 :(得分:2)
你可以使用
address = Regex.Replace(address, @"(?<=[02-9])ST$", " $&");
// Or, if `ST` appears in the start/middle of the string and is at the end of a "word"
address = Regex.Replace(address, @"(?<=[02-9])ST\b", " $&");
正则表达式匹配
(?<=[02-9])
- 与紧跟在 0
数字或 2
到 9
范围内的数字前面的位置匹配的正向后视ST
- ST
子串$
- 字符串结束和\b
- 一个词边界。