替换 wiki 中的 chemform - regexp [英] Replacing chemform in wiki - regexp

查看:40
本文介绍了替换 wiki 中的 chemform - regexp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能给我一些建议吗,我正在替换我的 wiki 中不再使用的 <chemform> 代码......字符串通常很简单:>

CH3COO-<chemform>Ba2+</chemform><chemform>H2CO3</chemform>

我需要用这些替换它们:

CH3COO-Ba2+H 子 2/子CO 3/子

到目前为止,我为 RegExr 工具想出了这个正则表达式:

匹配:]*>(\D*?)([0-9]*)(\D*?)(\D*?)([0-9]*)(\D*?)([-+]*?)

替换:$1$2$3$4$5$6$7

我知道代码很糟糕,但到目前为止它一直在为我工作,除了它让我得到像 <sub></sub> 这样的空字符串:

CH3COO-;<sub></sub>Ba<sub>2</sub><sup>+</sup>H<sub>2</sub>CO<sub>3</sub><sup></sup>

如何在不进行第二次替换搜索的情况下摆脱这些?非常感谢!

解决方案

您可以使用 Notepad++,它能够进行条件替换(您可以在 上一篇来自Wiktor Stribiżew).>

使用以下模式:

  • 匹配:([A-Za-z]+(?=[-+\d]))(?\d+)?(?[-+])?(?=[-+\w]*)
  • 替换:$1(?{sub}$+{sub})(?{sup}$+{sup})

根据您的输入样本,我得到:

CH3COO-<chemform>Ba<sub>2</sub><sup>+</sup></chemform><chemform>H<sub>2</sub>CO<sub>3</sub></chemform>

could you please give me some advice, I'm replacing the <chemform> code from my wiki which is not used any more... The strings are usually simple like these:

<chemform>CH3COO-</chemform>
<chemform>Ba2+</chemform>
<chemform>H2CO3</chemform>

I need them to be replaced by these:

CH<sub>3</sub>COO<sup>-</sup>
Ba<sub>2</sub><sup>+</sup>
H<sub>2</sub>CO<sub>3</sub>

So far I came up with this regexp for the RegExr tool:

match: <chemform\b[^>]*>(\D*?)([0-9]*)(\D*?)(\D*?)([0-9]*)(\D*?)([-+]*?)</chemform>

replace: $1<sub>$2</sub>$3$4<sub>$5</sub>$6<sup>$7</sup>

I know the code is horrible, but so far it's been working for me except for the fact it's getting me empty strings like <sub></sub>:

<sub></sub>CH<sub>3</sub>COO<sup>-</sup>
<sub></sub>Ba<sub>2</sub><sup>+</sup>
H<sub>2</sub>CO<sub>3</sub><sup></sup>

How can I get rid of these without doing second replace search? Thanks a lot!

解决方案

You could use Notepad++, which is able to proceed to conditional replacements (you can have details in that previous post from Wiktor Stribiżew).

Use the following patterns:

  • match: ([A-Za-z]+(?=[-+\d]))(?<sub>\d+)?(?<sup>[-+])?(?=[-+\w]*</chemform>)
  • replace: $1(?{sub}<sub>$+{sub}</sub>)(?{sup}<sup>$+{sup}</sup>)

Given your input sample, I get:

<chemform>CH<sub>3</sub>COO<sup>-</sup></chemform>  
<chemform>Ba<sub>2</sub><sup>+</sup></chemform>  
<chemform>H<sub>2</sub>CO<sub>3</sub></chemform>

这篇关于替换 wiki 中的 chemform - regexp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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