在VB 10中的特定点之后替换字符串 [英] Replacing strings after specific points in VB 10

查看:78
本文介绍了在VB 10中的特定点之后替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在文本文件中找到一个短语,并用其他替换它.尝试了很多东西,但总是以空手告终.

假设按此顺序有一个文本文件:

...
foob​​ar = 6000.0000
qwerty = 200.0000
poopxyz = 7000.0000
...

我要;
1)通过扫描整个文本找到短语(例如foobar),

2)用我想要的任何数字值替换"="之后和."之前的数字,即中间的"6000".

我不需要"="之前和."之后的部分,只需要中间的数字即可.

如果有人可以帮助我或给我可以执行此任务的代码,我将非常高兴.在此先感谢

平台:VB10/.net 4.0

I can''t figure out how to find a phrase in a text file, and replace it with something else. Tried lots of things but always ending up with empty hands.

Suppose there''s a text file in this order:

...
foobar=6000.0000
qwerty=200.0000
poopxyz=7000.0000
...

I want to;
1) Find the phrase (e.g. foobar) by scanning the whole text,

2) Replace the number coming after "=" and before ".", namely "6000" in the middle with any number value i want.

I don''t need the parts before "=" and after ".", only the number in the middle.

I''d be more than glad if anybody can assist me to or give me the code that can do this task. Thanks in advance

Platform: VB10 / .net 4.0

推荐答案

System.Text.RegularExpressions.Regex类的Replace 方法在此处解释http://msdn.microsoft.com/en-US/library/taz3ak2f%28v=vs.80%29 [ ^ ]为此,请使用模式(?<=foobar\s*=\s*)([^=.]+)(?=\s*\.),如果在foobar&之间有空格,则该模式将与单词匹配,如果该单词以foobar=开头,然后是.. =,=&所需字词,所需字词和.可以在这里 http://regexhero.net/tester/ [
The Replace method of System.Text.RegularExpressions.Regex class explained here http://msdn.microsoft.com/en-US/library/taz3ak2f%28v=vs.80%29[^] can be used for this purpose using the pattern (?<=foobar\s*=\s*)([^=.]+)(?=\s*\.) which matches a word if it is preceded by foobar= and followed by . even when there is white space between foobar & =, = & the required word, required word & . This pattern can be tested here http://regexhero.net/tester/[^].

To replace other combinations use variables for foobar and the replace string.
Dim fileText as String = System.IO.File.ReadAllText(fileName)
Dim modifiedText as String = System.Text.RegularExpressions.Regex.Replace( _
    fileText,"(?<=foobar\s*=\s*)([^=.]+)(?=\s*\.)","5000", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)


这听起来像是regex(正则表达式)的工作!

执行正则表达式替换将使您能够执行此操作,因为它支持相当复杂的通配符结构.

类似于以下内容的正则表达式字符串:

foob​​ar =.{1,} \.

替换值将是....foobar = newvalue.

所以:

foob​​ar = oldvalue.test

会变成

foob​​ar = newvalue.test

这是开始正则表达式的链接:

正则表达式快速入门 [
This sounds like a job for regex (Regular Expressions)!

Doing a regex replace will allow you to do this as it supports quite complex wild card structures.

A regex string similar to this:

foobar=.{1,}\.

replace value would be.... foobar=newvalue.

So:

foobar=oldvalue.test

Would become

foobar=newvalue.test

Here is a link on beginning regex:

Regular Expressions Quick Start[^]


这篇关于在VB 10中的特定点之后替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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