简体   繁体   English

正则表达式:字符之前的最后一次出现

[英]Regex: last occurrence of character before

In this comma delimited text I want to capture just the parts with 'match' in between, and the commas on either side 在此以逗号分隔的文本中,我只想捕获介于两者之间“匹配”的部分以及两边的逗号

text,something,hello,house,ymatchy,motor,xmatchx,yoyo

Currently I've got 目前我有

,.*?match.*?,

However it matches 但是它匹配

[1] ,something,hello,house,ymatchy,
[2] ,xmatchx,

I only want the first match the comma directly before the text, not from the beginning - I just want ,ymatchy, 我只希望第一个与逗号直接匹配的文本,而不是从开头开始-我只想要ymatchy,

Edit: 编辑:

Also how would it work if I was matching multiple characters, say the url encoding equivalent of a comma (%2C) 如果我匹配多个字符,也将如何工作,例如,URL编码等效于逗号(%2C)

text%2Csomething%2Cmatch%2Chello%2Chouse%2Cy%2match%2y%2Cmotor%2Cxmatchx%2Cyoyo

I would want to match 我想搭配

[1] %2Cmatch%2C
[2] %2Cy%2match%2y%2C
[3] %2Cxmatchx%2C

You could use the below regex, 您可以使用以下正则表达式,

,[^,]*match[^,]*,

DEMO 演示

If you want ,ymatchy, then remove the global flag. 如果需要,ymatchy,则删除全局标志。

Update: 更新:

%2C\w+match\w+%2C

DEMO 演示

I've managed to solve my 2nd half 我已经设法解决了我的第二半

%2C((?!%2C).)*match((?!%2C).)*%2C

I've marked Avinash Raj's answer as accepted, as he answered my original question. 我已将Avinash Raj的答案标记为已接受,因为他回答了我的原始问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式:一个字符的最后一次出现和另一个字符的第一次出现之间的匹配 - Regex: match between last occurrence of a character and first occurrence of another character 创建一个正则表达式来替换字符串中最后一次出现的字符 - Create a regex to replace the last occurrence of a character in a string 正则表达式删除除单个字符的最后一次出现以外的所有字符 - Regex to remove all but the last occurrence of a single character JavaScript RegEx-替换字符的第一个和最后一个出现 - JavaScript RegEx - replace first and last occurrence of a character 正则表达式在第一次出现字符之前获取所有内容 - Regex get all before first occurrence of character 使用正则表达式在关键字之前查找第一次出现的字符 - Find first occurrence of a character before a keyword with Regex 正则表达式:在最后一次出现之前过滤掉文本 - Regex: Filter out text before last occurrence 正则表达式删除除第一次出现和最后一次出现的特殊字符 - regex remove special character except first occurrence and last occurrence 正则表达式:确定在第一次出现另一个字符之前是否出现某个字符 - Regex: Determine if a character appears before first occurrence of another character 在最后一次出现特定字符串REGEX时拆分字符串 - Split a string at last occurrence of a particular character string REGEX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM