在VB.NET中替换括号中的字符串 [英] Replace string in brackets in VB.NET
本文介绍了在VB.NET中替换括号中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有带括号的字符串:
Dim A as String = "Example (example1)"
我想用另一个字符串替换括号中的字符串,所以我应该得到例如...
I want to replace string in brackets with another string so I should get for example...
A = "Example (example2)"
推荐答案
您可以使用 Regex.Replace 方法
Imports System.Text.RegularExpressions
Dim input As String = "Example (example1)"
Dim replacement As String = "example2"
Dim expression As New Regex(\(.*\))
Dim result As String = expression.Replace(input, replacement)
该模式将匹配两个括号之间的任何内容,包括其他括号(贪婪).调整表达式
以满足您的需要.
That pattern will match anything in-between two parenthesis including other parenthesis (greedy). Adjust expression
to suit your need.
这篇关于在VB.NET中替换括号中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文