【问题标题】:How to use regex pattern as replacement如何使用正则表达式模式作为替换
【发布时间】:2014-04-01 19:47:31
【问题描述】:

我有一个替换字符串:

********is******ri****f******s**d******s****du****?

和一个搜索模式:

([y\s]|[wh]|[apl]|[oo]|[mb])*

和替换模式:

*$2*

是否有可能找出原始字符串是什么,或者使用替换模式来反转它?

【问题讨论】:

  • 所以********is******ri****f******s**d******s****du****是替换的结果,你在找原来的字符串吗?
  • 如果您不能手动操作(您不能),您的计算机也将无法完成。你唯一能做的就是生成所有可能的原始字符串,这将是一个粪便负载(我猜是无限的)。
  • 这像正则表达式加密还是什么?这很奇怪——我以前从未想过使用正则表达式来加密某些东西。
  • 这与加密无关。加密的重要部分是您可以使用适当的密钥对其进行解密。您的示例只是关于信息丢失。这就像说“用 X 替换每个元音”。您无法撤消此操作,它不是加密。
  • 那么它像字谜吗?

标签: javascript python regex string pcre


【解决方案1】:

由于正则表达式中有许多替代项,如果您想反转操作,将不会只有一个结果,因为正则表达式包含这些替代项:

1st Alternative: [y\s]
[y\s] match a single character present in the list below
    y the literal character y (case sensitive)
    \s match any white space character [\r\n\t\f ]
2nd Alternative: [wh]
[wh] match a single character present in the list below
    wh a single character in the list wh literally (case sensitive)
3rd Alternative: [apl]
[apl] match a single character present in the list below
    apl a single character in the list apl literally (case sensitive)
4th Alternative: [oo]
[oo] match a single character present in the list below
    oo the literal character o (case sensitive)
5th Alternative: [mb]
[mb] match a single character present in the list below
    mb a single character in the list mb literally (case sensitive)

因此您也许可以编写代码来为您提供所有可能的组合(巨大的负载),但不是一个结果。 希望这会有所帮助。

【讨论】:

  • 4.5259256e41 这个小字符串的可能性。祝你好运!
猜你喜欢
  • 2012-02-12
  • 2018-08-03
  • 2016-07-17
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
相关资源
最近更新 更多