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!

enter image description here

from strings above want match

  1. while #enginespeed
  2. set #waitstart
  3. set #waitstart<13
  4. set #waitstart<=13
  5. 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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -