获取2个字符串之间的字符 [英] Get string between 2 strings

查看:97
本文介绍了获取2个字符串之间的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个字符串之间获取字符串。我将用于网址



例如



I want go get string between two strings. I will use for url

For example

dim main as string = "kgjhnbkfm glnvfkdşlv http://ghkng.xfg/ngbf.png gvlkdjg"
dim before as string="http://"
dim after as string = ".png"
dim useless as string='I need to get between theese strings (before and after)
dim urlfinal as string=before+useless+after

推荐答案

使用正则表达式:

Use a Regex:
Dim regex As Regex = New Regex( _
      "(?<=http://).*?(?=\.png)", _
    RegexOptions.Multiline _
    Or RegexOptions.CultureInvariant _
    Or RegexOptions.IgnorePatternWhitespace _
    Or RegexOptions.Compiled _
    )
Dim InputText As String = "kgjhnbkfm glnvfkdşlv http://ghkng.xfg/ngbf.png gvlkdjg"
Dim m As Match = regex.Match(InputText)
If (m.Success) Then
    Console.WriteLine(m.Value)
End If


您可以使用rexex:

Expresso - 用于构建和测试正则表达式的工具 [ ^ ]

制作预先安装的部分。
You could use rexex for this:
Expresso - A Tool for Building and Testing Regular Expressions[^]
MAtch the part that is preseded by and followed by.


我建​​议你看看正则表达式 [ ^ ]。



一个快速的C#解决方案(也许不是bes因为我的RegExp不如它应该的那么好):



I would suggest you look at regular expressions[^].

A quick C# solution (maybe not the best as my RegExp is not as good as it should be):

string main = "kgjhnbkfm glnvfkdşlv http://ghkng.xfg/ngbf.png gvlkdjg";
Regex pattern = new Regex("(.*)http://(?<urlfinal>(.*)).png(.*)");

Match match = pattern.Match(main);

string urlFinal = "http://" + match.Groups["urlfinal"].Value + ".png";
</urlfinal>





应该可以轻松转换为VB.NET。



It should be easily convertible to VB.NET.


这篇关于获取2个字符串之间的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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