blackened
blackened

Reputation: 903

Regex: Only capture if there is (are) characters between the searched characters

In my LaTeX work I need to do Regex search with \|(.*?)\| to capture |whatever| and replace it with \somecommand{$1}. But I do not want to capture || (That is, there is nothing between them.) How should I refine my regex search?

(By the way, what should my title be, so that it is useful for others?)

Upvotes: 0

Views: 26

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

You have to change the asterix (which matches 0+ times) to a plus sign make the quantifier match at least 1 character.

\|(.+?)\|
    ^

Upvotes: 1

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

Change your regex to,

\|[^|]+\|

OR

\|.+\|

If you want to also capture pipes in between searched content

Upvotes: 2

Related Questions