HattrickNZ
HattrickNZ

Reputation: 4653

regx - extracting everything between an underscore and a dot from text

I am trying to extract everything between the _ and the . in this text:

CIL20150424_NSGS02RPK.dat
CIL20150424_NSU01MAH.dat

to give me these matches:

NSGS02RPK
NSU01MAH

I am using this site and the best I can do is locate the _ using the negitive look ahead:

\d(_=?)

but this selects the 4 also 4_.

How do I just select everything between the _ and the . in this text?

Upvotes: 2

Views: 1214

Answers (4)

Federico Piazza
Federico Piazza

Reputation: 30995

You can use a simple regex like below and grab the content from the capturing group:

_(.*)[.]
or
_(.*)\.     it's up to you to choose what you like most

Working demo

Regular expression visualization

Match information

MATCH 1
1.  [12-21] `NSGS02RPK`
MATCH 2
1.  [38-46] `NSU01MAH`

Upvotes: 0

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22637

[^_]+_([^\.]+)\.

maybe i'm missing something. this skips everything up to the underscore, then captures everything between the underscore and the period.

Upvotes: 0

Billy Moon
Billy Moon

Reputation: 58531

I would do it by capturing the part you want as a group _([^.]+)

enter image description here

Upvotes: 0

Bohemian
Bohemian

Reputation: 425033

This regex matches your target:

(?<=_).*?(?=\.)

See live demo.

The regex uses a look behind for the underscore, a reluctant quantifier (in case there's more than one per line), and a look ahead for a dot (which you must escape to match a literal dot).


btw your attempt at a look ahead (_=?) is not a look ahead, it's a captured (bracketed) underscore followed by an optional equals sign.

Upvotes: 2

Related Questions