Regex to match a string not followed by some string -
i have string block below
1. while #enginespeed$b4tgup# 2. while #acceleratorpedal$desddd# <=2 3. while #acceleratorpeda@$desddd# <=2 4. while #acceleratorpe#al$desddd# <=2 5. while #acceleratorp@dal$desddd# <=2 6. while #acceleratorpeda l$desddd# <=2 7. 设置 #acceleratorpedal$y6qs8m#=@acceleratorpedal+0.06 8. 设置 #waitstart$jhx0vu#=11 9. 设置 #waitstart$jhx0vu#^11 10. 设置 #waitstart$jhx0vu#_11 11. 设置 #waitstart$jhx0vu#-11 12. 设置 #acceleratorquora$yd6ba3#=1 13. 设置 #acceleratorquora$yd6bd3#=1 14. 15. check #enginepower# 16. while #enginespeed 17. set #waitstart 18. set #waitstart<13 19. set #waitstart<=13 20. set #waitstart <= 13 now want have regex match substring starts # not followed # or not followed substring $\w{6}#.
so in string block above line 16,17,18,19,20 match,in line 16 result #enginespeed,in line 17 result #waitstart,the others not match, in line 20 ,the result #waitstart, followed <= 13,not followed # or $\w{6}#,so it's match!
for example,in line 15 string check #enginepower#,it starts #,but followed #,in line 13 string 设置 #acceleratorquora$yd6bd3#=1,it starts #,but followed substring $\w{6}#.
i have wrote regex #\w+(?<!#|$\w{6}#),but test result in regexbuddy shown in picture below,almost every line match,it doesn't meet requirement,how can can modify regex? in advance!
from strings above want match
- while #enginespeed
- set #waitstart
- set #waitstart<13
- set #waitstart<=13
- set #waitstart <= 13
you can use
(?<=\s)#\w+(?!.*[@$#]) see this regex demo
details:
(?<=\s)- there must whitespace before...#- literal hash\w+- 1 or more word chars(?!.*[@$#])- fail match if there@, or$, or#somewhere after\w+on line.

Comments
Post a Comment