I have been trying to write a regular expression for this but so far did not able to succeed.
_ any thing\_ fdfdf \_ any thing_
underscore then any characters until _.
\_ is an escape character so the regular expression must accept string like this.
_ any \_ thing _
the following string:
checking_ happens \_ ano\_ther _ test of bold _ and escape \_asteric
should give:
_ happens \_ ano\_ther _
So far I am only able to come up with this:
(\\_)|_[^_]*[\\_]*[_]
This does the job:
Breaking it up:
(?<!\\)(?:\\\\)*– Match an even number of backslashes not preceded by more backslashes_– followed by an underscore((?:[^_\\]|\\.)+)– Match either of the following 1 or more times[^_\\]– Any character except an underscore or backslash\\.– Any backslash / character pair (e.g.\_or\\)_– Match the trailing underscoreThis will capture the string between the underscores in its first group.