0

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?

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Worked, Thanks a lot
0

Regex: \s Substitution: |

Details:

  • \s Matches any whitespace character

Regex demo

VB.NET code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.