You need to enable JavaScript to run this app. 如何用正则提取从OAuth oauth_con开头到结尾且不含反斜杠的内容
优惠活动
大模型
产品
解决方案
定价
更多

如何用正则提取从OAuth oauth_con开头到结尾且不含反斜杠的内容

实现思路

先从原始文本中提取出以OAuth oauth_consumer_key开头到末尾双引号之前的内容,再删除提取结果里所有的反斜杠即可。

正则匹配规则

你可以使用正则表达式OAuth oauth_consumer_key.*?(?="\s*$)匹配目标片段,匹配完成后全局替换反斜杠\为空即可得到结果。

不同语言实现示例

JavaScript 实现

// 原始输入文本
const rawStr = `"oauth":"OAuth oauth_consumer_key=\\"asd-asd-asd-\\", oauth_nonce=\\"asdasdjjasdasd\\", oauth_signature=\\"asdasd%sdfsdf\\", oauth_signature_method=\\"HMAC-SHA1\\", oauth_timestamp=\\"234234\\", oauth_version=\\"1.0\\""`
// 匹配目标片段
const matched = rawStr.match(/OAuth oauth_consumer_key.*?(?="\s*$)/)
if (matched) {
  // 剔除所有反斜杠
  const res = matched[0].replace(/\\/g, '')
  console.log(res)
}

Python 实现

import re

# 原始输入文本
raw_str = '"oauth":"OAuth oauth_consumer_key=\\"asd-asd-asd-\\", oauth_nonce=\\"asdasdjjasdasd\\", oauth_signature=\\"asdasd%sdfsdf\\", oauth_signature_method=\\"HMAC-SHA1\\", oauth_timestamp=\\"234234\\", oauth_version=\\"1.0\\""'
# 匹配目标片段
matched = re.search(r'OAuth oauth_consumer_key.*?(?="\s*$)', raw_str)
if matched:
    # 剔除所有反斜杠
    res = matched.group().replace('\\', '')
    print(res)

输出结果

运行上述代码后即可得到期望输出:

OAuth oauth_consumer_key="asd-asd-asd", oauth_nonce="asdasdjjasdasd", oauth_signature="asdasd%sdfsdf", oauth_signature_method="HMAC-SHA1", oauth_timestamp="234234", oauth_version="1.0"

内容的提问来源于stack exchange,提问作者Smile Karthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 09:36:08