I'm looking for a way to match the second last word on a line, such as this:
123 Smith St Melbourne VIC 3000
I'd like to match just "VIC". Does someone have a regex I can use?
I'm looking for a way to match the second last word on a line, such as this:
123 Smith St Melbourne VIC 3000
I'd like to match just "VIC". Does someone have a regex I can use?
Depending of what is a "word" for you, you can use:
\S+(?=\h+\S+$) will match 1 or more not space followed by 1 or more horizontal space then 1 or more non space[a-zA-Z]+(?=\h+[a-zA-Z]+$)[a-zA-Z0-9]+(?=\h+[a-zA-Z0-9]+$)\pL+(?=\h+\pL+$)[\pL\pN]+(?=\h+[\pL\pN]+$)To match the second word from the end use: (?!(\w+\s\w+$))|(?:.+?)
To replace everything other than the second last word use: (?!(\w+\s\w+$))(?:.+?)
You might need to do something about special characters you may have, though.