2

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!

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
flyingfox
  • 13,414
  • 3
  • 24
  • 39

1 Answers1

3

From all the strings above you want to 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 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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • thanks a lot,I have updated my question, now if I also want to mach a string like this **set #WaitStart <= 13**, how can I modify your regex,I found your regex only match a substring not followed by anything! – flyingfox Jul 13 '16 at 03:01
  • Now I have wrote a regex for myself `\s#\w+(?![@\w\$#_])`,but thank you all the same! Shall I need to vote your answer for the right answer due to your patient? – flyingfox Jul 13 '16 at 06:43
  • I do not understand how `\s#\w+(?![@\w\$#_])` can work for you since it matches in `6. While #AcceleratorPeda l$desddd# <=2` (see [demo](https://regex101.com/r/rN3mP9/6)). If my answer proved helpful, please consider upvoting, if it worked for you, consider accepting (these are usual SO rules). – Wiktor Stribiżew Jul 13 '16 at 06:52
  • Ah, I did not get your update. You do not want the `<=` + digits in the match, right? I updated the answer. – Wiktor Stribiżew Jul 13 '16 at 06:53
  • I was battling this in Ruby, and the `[]` were the key to unlock why it wasn't working. (I was pushing forward with `(?!\S)` and so-on) – joshfindit May 05 '20 at 19:10
  • @joshfindit Well, I have no idea what you actually need, but from today's perspective, my solution for the current OP problem should be `(?<!\S)#(?!.*[@$#])\w+` ([demo](https://regex101.com/r/rN3mP9/15)). – Wiktor Stribiżew May 05 '20 at 20:22