从文本框中查找和替换一系列文本 [英] Find and replace a range of text from a text box

查看:30
本文介绍了从文本框中查找和替换一系列文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进入Visual Basic 项目,我试图替换来自文本框URL的一部分.例如,我有这个 https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg URL 到 TextBox1 并且我想替换部分 <代码>/s912- 和 /s1600-.

Into a Visual Basic project I am trying to replace a part of URL coming from a text box. For example, I have this https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg URL into TextBox1 and I want to replace the part /s912- with /s1600-.

我是通过这样做的:Dim url = Replace(TextBox1.Text, "/s912-", "/s1600-").但是,这个网址每次都不一样.例如,/s912- 部分可以是 /s800-.所以我的下一步是使用星号:Dim url = Replace(TextBox1.Text, "/s*-", "/s1600-").当然是行不通的!所以我需要语法方面的帮助或更好的主意.

I did it by doing something like this: Dim url = Replace(TextBox1.Text, "/s912-", "/s1600-"). But, this URL isn't the same every time. The part /s912- coould be /s800- for example. So my next though is to use an asterik: Dim url = Replace(TextBox1.Text, "/s*-", "/s1600-"). Of course it doesn'r work! So I need help with the syntax or a better idea.

推荐答案

Regex.Replace 可用于使用 正则表达式.

Dim input = "https://lh3.googleusercontent.com/blahblahblah/s912-Ic42/blahblahblah.jpg"
Dim output = Regex.Replace(input, "/s\d+-", "/s1600-")

输出是:

https://lh3.googleusercontent.com/blahblahblah/s1600-Ic42/blahblahblah.jpg

这篇关于从文本框中查找和替换一系列文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆