简体   繁体   English

vb.net RegEx替换

[英]vb.net RegEx replace

I have a regex that searches an input string looking for a possible SSN. 我有一个正则表达式,用于搜索输入字符串以查找可能的SSN。 That part all works great, but I want to be able to replace what I detect as a SSN with a string of asterisks. 这部分都很好用,但是我希望能够用一串星号代替我检测到的SSN。

For example, if 123456789 is my SSN to replace and I use "123456789, 00123456789000, 1234567899999" as the input string, I just want to end up with "*********, 00123456789000, 1234567899999" but everything I am trying is affecting the second and third string elements as well. 例如,如果123456789是我的SSN更换,我用"123456789, 00123456789000, 1234567899999"作为输入字符串,我只是想落得"*********, 00123456789000, 1234567899999"但我的一切我正在尝试影响第二和第三字符串元素。

I was thinking that I could use my initial search pattern as the same replace pattern but also make sure there wasn't a digit on each side of it but I can't get it to work. 我当时想我可以将最初的搜索模式用作相同的替换模式,但还要确保它的每一侧都没有数字,但是我无法使其正常工作。

This is my search pattern and it works fine: 这是我的搜索模式,它可以正常工作:

Dim reg As New Regex("\d{3}\D{0,1}\d{2}\D{0,1}\d{4,}") 

您可以使用\\b ...确定模式前后的字边界。

"\b\d{3}\D{0,1}\d{2}\D{0,1}\d{4}\b"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM