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
  • 561,645
  • 34
  • 376
  • 476
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
  • 713,503
  • 59
  • 514
  • 593