NRB
NRB

Reputation: 3

Replace pattern with pattern in vb.net string

I want to replace "0A ","0B ",...,"1A ","1B ",... patterns with "0A|","0B|",...,"1A|","1B|",... from string vb.net

I can write individual replace lines like

string = string.Replace("0A ", "0A|")
string = string.Replace("0B ", "0B|")
.
.
.
string = string.Replace("0Z ", "0Z|")

But, I would have to write too many lines(26*10*2- Two because such scenario occurs twice) and it just doesn't seem to be a good solution. Can someone give me a good regex solution for this?

Upvotes: 0

Views: 2402

Answers (2)

Srdjan M.
Srdjan M.

Reputation: 3405

Regex: \s Substitution: |

Details:

  • \s Matches any whitespace character

Regex demo

VB.NET code:

Regex.Replace("0A ", "\s", "|") Output: 0A|

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522807

Use Regex.Replace:

result = Regex.Replace(string, "(\d+[A-Z]+) ", "$1|")

I used the pattern \d+[A-Z]+ to represent the text under the assumption that your series of data might see more than one digit/letter. This seems to be working in the demo below.

Demo

Upvotes: 1

Related Questions