I have a regular expression question.

Asked 1 years ago, Updated 1 years ago, 38 views

There's an address like 'media/dir1/dir2/file.png', except for the file name I want to extract only this part of media/dir1/dir2/

'.+(?<=/)' This regular expression succeeded.

But I don't know exactly why I succeeded.

In particular, if '(?<=/)' rear search was successful in the rear search, why only the rear search disappeared? I wonder if the front part including / comes out as a result.

regex

2022-09-21 11:37

1 Answers

in front of the parentheses subexpression ((...))?If =, ?!, ?<= or ?<! appears, lookaround is applied. At this point, the matched area in subexpression will leave as an empty character.

Therefore, .+(?<=/) treats the last matching / as an empty character and outputs all characters before that.

In addition, forward searching seems to be more appropriate in meaning than backward searching here.

.+(?=/)

The meaning of the forward search expression above means that all characters must be printed before the last / character is found.


2022-09-21 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.