0

我想获得一个工作代码,以简单地从文本行中删除始终"("")".

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

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

丢失...

谢谢

4

3 回答 3

1

使用正则表达式的一种方法;

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
于 2013-03-28T11:02:08.170 回答
1

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

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

pos = InStr(msg, "(")

If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))
于 2013-03-28T11:42:48.437 回答
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

如果是在句子的中间,使用 split 函数两次:

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。多个实例我会拆分句子,然后分解包含“()”的每个部分并连接最后一个句子。

于 2016-12-17T19:40:52.753 回答