Reputation: 13506
I have a string block as 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 I want to have a regex to match a substring starts with #
but not followed by #
or not followed by a substring like this $\w{6}#
.
So in the string block above only line 16,17,18,19,20 match,in line 16 the result is #EngineSpeed,in line 17 the result is #WaitStart,the others are not match, in line 20 ,the result is also #WaitStart, it followed by <= 13
,not followed by #
or $\w{6}#
,so it's also match!
For example,in line 15 the string is Check #EnginePower#,it starts with #
,but it followed by #
,in line 13 the string is 设置 #AcceleratorQuora$yd6bd3#=1,it starts with #
,but followed by a substring like $\w{6}#
.
I have wrote a regex like this #\w+(?<!#|$\w{6}#)
,but the test result in RegexBuddy is shown in the picture below,almost every line is match,it doesn't meet my requirement,how can I can modify the regex? Thanks in advance!
Upvotes: 2
Views: 478
Reputation: 627536
From all the strings above you want to 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 be a whitespace before...#
- a literal hash\w+
- 1 or more word chars (?!.*[@$#])
- fail the match if there are @
, or $
, or #
somewhere after the \w+
on the line.Upvotes: 3