简体   繁体   English

正则表达式,仅当某个字符出现在特定数量的字符之后时才删除该特定字符之后的所有内容

[英]Regex for removing everything after a certain character only if the character occurs after a specific number of characters

I'm looking for a simple regular expression for this, 我正在为此寻找一个简单的正则表达式,

Example 1: 范例1:

n = 5

Input: abcde@varun@gmail.com

Output: abcde@varun

Example 2: 范例2:

n = 5

Input: abcd@varun@gmail.com

Output: abcd@varun@gmail.com 

This means that if n = 5 and if the symbol @ is NOT occuring on or before the 5th index(Assuming the string starts at index 1) then remove all everything(including the @ symbol) after the second occurance of @ . 这意味着,如果n = 5,如果符号@ 为ON或第五索引之前存在的(假设字符串在索引1处开始),然后卸下的第二次数毕竟一切(包括@符号)@。

Leave the string as it is if the above rule is not satisfied. 如果不满足上述规则,则保留字符串原样。

Thanks Varun. 谢谢瓦伦。

You can use this regex to capture the part that you wish to keep into the first group: 您可以使用此正则表达式将要保留的零件捕获到第一组中:

(^[^@]{5,}@[^@]*)@

The expression matches a string that has at least two @ signs, with the first @ not occurring within the initial five characters. 该表达式匹配的字符串至少具有两个@符号,第一个@不会出现在开头的五个字符内。

This would capture abcde@varun , but not abcd@varun , because @ occurs within the initial five characters. 这将捕获abcde@varun ,但不会捕获abcd@varun ,因为@出现abcd@varun五个字符内。

Demo. 演示

Replace 5 with n to change the length of the prefix as needed. n替换5以根据需要更改前缀的长度。

^(?!(?:@|.@|.{2}@|.{3}@|.{4}@))(.*?@.*?)@.*$

Try this.Replace by $1 .See demo. 试试这个,用$1代替。

https://regex101.com/r/uC8uO6/7 https://regex101.com/r/uC8uO6/7

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

相关问题 正则表达式删除某个字符后的所有内容(注释) - regex to remove everything after a certain character (comment) 删除字符后的所有内容,包括该字符 - Removing everything after a character, including that character Python:删除所有内容,包括一行中的某个字符以及该字符之后的所有内容 - Python: removing everything including and after a certain character on a line RegEx-在特定字符(#)之后获取所有字符 - RegEx - Get All Characters After A Specific Character (#) 正则表达式重命名所有文件递归删除字符“?”命令行后的所有内容 - Regex to rename all files recursively removing everything after the character “?” commandline 正则表达式用于在匹配后删除一定数量的字符 - Regex for remove a certain number of character after a match 正则表达式忽略字符后的所有内容 - Regex ignore everything after a character 正则表达式仅在特定字符后匹配字符串 - Regex match strings only after certain character 仅当数字在该字符之前时,才删除幕字符之后的所有字符 - Removing all characters after curtain character only if digit is before that character PHP正则表达式,匹配某个字符后的所有内容并以换行符结尾 - PHP Regex that matches everything after a certain character and ends with newline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM