[英]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
五个字符内。
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.