Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex start searching from the end of the string (reverse)

Tags:

regex

I have strings that have blocks enclosed in underscores in them. Example:

*Text* _word_ it is something we read every day. _Words in texts_ can be really expressive. _A nice text is a pleasure for your body and soul_ (Oscar Wilde)

In the example above there are three such blocks but the number varies from string to string. I want to match only the last one, i.e. starting from the end of the line lazily skip characters until the first _ is found, skip any following characters until encountering the second _ and stop right there.

It is easy to to find a similar block if we were looking for the very first one inside the string, but how about finding the last one?

like image 326
Захар Joe Avatar asked Oct 23 '14 13:10

Захар Joe


People also ask

How do I reverse a string in regex?

string first(sm[1]); string second(sm[2]); reverse(first. begin(), first. end()); reverse(second. begin(), second.

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What is \r and \n in regex?

\n. Matches a newline character. \r. Matches a carriage return character.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \.


Video Answer


2 Answers

The text between the second last _ and the end of the string should be matched

Use a negated character class, like

([^.]*$)

It will match everything from the end of the string that isn't ., resulting in the last quote (assuming each quote ends with a .)

http://regex101.com/r/fA3pI7/1

like image 100
ʰᵈˑ Avatar answered Sep 20 '22 06:09

ʰᵈˑ


Have a try with:

((?:_[^_\r\n]*){2})$

It matches an underscore followed by any number of any character that is not underscore or line break, all that occurs twice before the end of lien.

like image 24
Toto Avatar answered Sep 22 '22 06:09

Toto