You need to enable JavaScript to run this app. 使用Regex匹配单字符串内多模式:提取指定分隔符间的申请人姓名
优惠活动
大模型
产品
解决方案
定价
更多

使用Regex匹配单字符串内多模式:提取指定分隔符间的申请人姓名

正则提取申请人姓名解决方案

可行性说明

完全可以通过正则实现,你的需求是提取固定前缀和固定后缀之间的内容,属于正则的典型适用场景。

可直接使用的正则表达式

applicant\(s\)\.: (.*?)(?=,Please)

语法解释(适合入门使用者理解)

  • applicant\(s\)\.: :匹配你指定的前缀内容,其中(、)、.都是正则的特殊语法字符,所以前面加\转义,才能匹配文本里的原始符号
  • (.*?):小括号代表捕获组,就是你最终要提取的姓名内容;.*?是非贪婪匹配,会尽可能短的匹配字符,避免一次性吞掉多段符合规则的内容
  • (?=,Please):正向预查语法,作用是判定匹配到后面跟着,Please时就停止,且不会把,Please纳入最终匹配结果,不需要你后续手动裁剪字符串

常见语言调用示例

Python 示例

import re

# 你的原始文本
raw_text = "Please enter the name of your school.: Test Primary School,Please enter the name(s) of your applicant(s).: Sandra McTest,Please enter the name of your school.: Test Primary School,Please enter the name(s) of your applicant(s).: Grace Test, Brad Test, Lovelace Test,Please enter the name of your school.: Test Primary School,"
# 正则规则,r前缀代表原生字符串,不用额外处理转义
pattern = r'applicant\(s\)\.: (.*?)(?=,Please)'
# 提取所有符合规则的内容
result = re.findall(pattern, raw_text)
print(result)
# 输出:['Sandra McTest', 'Grace Test, Brad Test, Lovelace Test']

JavaScript 示例

// 你的原始文本
const rawText = "Please enter the name of your school.: Test Primary School,Please enter the name(s) of your applicant(s).: Sandra McTest,Please enter the name of your school.: Test Primary School,Please enter the name(s) of your applicant(s).: Grace Test, Brad Test, Lovelace Test,Please enter the name of your school.: Test Primary School,";
// 正则规则,末尾的g代表全局匹配,提取所有符合规则的内容
const pattern = /applicant\(s\)\.: (.*?)(?=,Please)/g;
// 提取结果
const result = [...rawText.matchAll(pattern)].map(item => item[1]);
console.log(result);
// 输出:['Sandra McTest', 'Grace Test, Brad Test, Lovelace Test']

入门使用注意事项

  • 不要漏写特殊字符的转义符\,否则会出现匹配不到内容的问题
  • 必须使用非贪婪匹配.*?,如果写成贪婪匹配.*,会从第一个前缀一直匹配到最后一个后缀,所有内容会被合并成一条结果,不符合需求
  • 如果你的文本里后缀有其他变体,可以调整(?=)里的内容适配边界规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 03:09:04