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?
string first(sm[1]); string second(sm[2]); reverse(first. begin(), first. end()); reverse(second. begin(), second.
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.
\n. Matches a newline character. \r. Matches a carriage return character.
\\. 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 \.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With