从文本中删除所需的内容

时间:2013-03-28 10:52:48

标签: vbscript

我希望获得一个有效的代码,只需从文本行中删除始终以"("开头并以")"结尾的特定部分。

示例文字:Hello, how are you (it is a question)

我想删除此部分:"(it is a question)"仅保留此消息"Hello, how are you"

忘记...

由于

3 个答案:

答案 0 :(得分:1)

使用Regular Expressions的一种方式;

input = "Hello, how are you (it is a question)"

dim re: set re = new regexp
with re
    .pattern = "\(.*\)\s?" '//anything between () and if present 1 following whitespace
    .global = true

    input = re.Replace(input, "") 
end with

msgbox input

答案 1 :(得分:1)

如果要删除的部分始终位于字符串的末尾,则字符串操作也会起作用:

msg = "Hello, how are you (it is a question)"

pos = InStr(msg, "(")

If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))

答案 2 :(得分:0)

如果句子总是以()部分结尾,请使用split函数:

line = "Hello, how are you (it is a question)"

splitter = split(line,"(")        'splitting the line into 2 sections, using ( as the divider
endStr = splitter(0)              'first section is index 0

MsgBox endStr                     'Hello, how are you

如果它在句子的中间,请使用拆分功能两次:

line = "Hello, how are you (it is a question) and further on"

splitter = split(line,"(")
strFirst = splitter(0)             'Hello, how are you

splitter1 = split(line,")")        
strSecond = splitter1(UBound(Splitter1))    'and further on

MsgBox strFirst & strSecond        'Hello, how are you and further on

如果只有一个“()”实例,则可以使用“1”代替UBound。 多个实例我会拆分句子,然后分解包含“()”的每个部分并连接最后一句。