1

Considering a string like:

 "@foo #foo@foo@foo #foo\n
  foofoofoo\n 
  foo @bar"

I try for 2 days to extract the last #/@ occurrence so here, the @ before 'bar'. For now, i have something like this [@#](?!.*[@#]) which seems to work except when user insert new lines in there.

Can someone give me some tips please?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
nantes_flo
  • 13
  • 3

1 Answers1

0

You can use this lookhahead regex:

/[#@](?![\s\S]*[#@])/

RegEx Demo

(?![\s\S]*[#@]) is the negative lookahead that asserts there is no @ or # ahead of current position in any line. [\s\S] matches any character including newline.

anubhava
  • 761,203
  • 64
  • 569
  • 643