【发布时间】:2020-06-20 23:25:59
【问题描述】:
大家好,和许多人一样,我正在将我的新冠病毒时间转化为编码。共病? :)
我需要在 Excel 中使用 VBA 从字符串中提取子字符串,并希望获得有关可用解决方案的建议。我认为正则表达式将是要走的路,但实际上我很不确定,因为我对正则表达式相当不熟悉,这可能很复杂。也许有一些我不知道的更简单的解决方案,非常感谢任何建议。
字符串源自开源 mybb 论坛的英文文件。我正在创建一个 Excel 工作簿来帮助翻译人员,我将把它捐赠给他们的社区。p>
需要的是移除 html,并将每个子字符串放置在相邻的单元格中。模式是:
- 之前
- 在 > 和 之间
- 后面的文字>
字符串示例(不带首尾引号):
示例 1:
"You are currently viewing a stripped down version of our content. <a href=\"{1}\">View the full version</a> with proper formatting."
- 字符串 1 =
"You are currently viewing a stripped down version of our content. " - 字符串 2 =
"View the full version" - 字符串 3 =
" with proper formatting."
示例 2:
"<b>Private</b> Only you will be able to view this event. (Registered Users Only)."
- 字符串 1 =
"Private" - 字符串 2 =
" Only you will be able to view this event. (Registered Users Only)."
示例 3:
" This day does not have any events associated with it.<p><a href=\'calendar.php?action=addevent&calendar={1}&day={2}&month={3}&year={4}\'>Post an Event</a>.</p>"
- 字符串 1 =
"This day does not have any events associated with it." - 字符串 2 =
"Post an Event" - 字符串 3 =
"."
示例 4:(这个示例是我见过的最大的示例)
"<p><br />[list]<br />[*]List Item #1<br />[*]List Item #2<br />[*]List Item #3<br />[/list]<br /><ul><li>List item #1</li><li>List item #2</li><li>List Item #3</li>"
- 字符串 1 =
"[list]" - 字符串 2 =
"[*]List Item #1" - 字符串 3 =
"[*]List Item #2" - 字符串 4 =
"[*]List Item #3" - 字符串 5 =
"[/list]" - 字符串 6 =
"List item #1" - 字符串 7 =
"List item #2" - 字符串 8 =
"List item #3"
任何建议将不胜感激。
编辑:添加更多示例
<span title=\"{1}\">Today</span>
<span title=\"{1}\">Yesterday</span>
<span title=\"{5}{6}\">{1}{2} {3} {4}</span>
You are currently using <strong>{1}</strong>.
<br /><br />You are encouraged to register; once you register you will be able to post messages, set your own preferences, and maintain a profile.
<br /><br />Some of the features that generally require registration are subscriptions, changing of styles, accessing of your Personal Notepad and emailing forum members.
<br /><br />Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk.
<br /><br />Cookies on this forum also track the specific topics you have read and when you last read them.
<p><br />[url]http://www.example.com/[/url]<br /> <a href=\"http://www.example.com/\">http://www.example.com/</a>
<p>[url=http://www.example.com/]Example.com[/url]<br /> <a href=\"http://www.example.com/\">Example.com</a>
<p>[email]example@example.com[/email]<br /> <a href=\"mailto:example@example.com\">example@example.com</a>
【问题讨论】:
-
如果您只是在 .text 内容之后,则不需要正则表达式。使用 html 解析器并提取文档文本。有源网址吗?确实有点依赖 html 的质量,因为 vba html 解析器实现并不是非常宽容。但是正则表达式也会受到影响,并且通常是处理 html 的糟糕选择。
-
我需要将每个子字符串放在相邻的单元格中,这样我设计的翻译系统才能正常工作。最终操作将用原始字符串的编辑器翻译替换该字符串。这些文件在 mybb 的 inc\languages\english 中可用:mybb.com/download。我已经删除了这些文件中等号之前的所有内容并进行了分类,因此上面示例中的字符串比原始文件中的文本更适合我。
-
你让我走上了正轨,谢谢!我在这里找到了 Todds 的回复,这可以解决问题。 stackoverflow.com/questions/5327512/…
-
不要忘记您可能应该捕获原始文本中每个提取部分的起始位置 - 如果有机会,您不能只用翻译替换(例如)原始字符串中的字符串text 可能是未提取部分的一部分。例如“span”、“table”等