Hanfei Sun
Hanfei Sun

Reputation: 47051

Non-greedy regex in emacs like this

The text is like this:

 /mnt/alphabets....../alphabets..../dataset974/974_summits.bed

I want to select this part:

 /mnt/alphabets....../alphabets..../dataset974

Using regex I-search, I typed in:

 /mnt.*[0-9]*?

But the selected part is:

/mnt/alphabets....../alphabets..../dataset974/974_summits.bed

Does anyone how to do this?

Upvotes: 0

Views: 634

Answers (2)

yibe
yibe

Reputation: 4109

The problem with your regex is that /mnt.* greedily eats up the whole text, from /mnt to the end of the line. What should be non-greedy is the first *. Try this:

/mnt/.*?[0-9]+

Upvotes: 3

prageeth
prageeth

Reputation: 7395

Why don't you use groups? For an example in following regEx, group 1 gives you the required result.

(/mnt(?:.*))/

Upvotes: 0

Related Questions