Sai
Sai

Reputation: 311

Generating Regular Expression Parsing

I need to convert the four lines below into a single regular expression.

piece_id="E00401007758725D"
PieceID = E00401007758725D 
Piece=E00401007758725D
piece E00401007758725D

I've tried the following:

[pP]iece[_]*?[iI]*[dD]* ?[=]* ?"?(?<PieceID>[A-Z0-9]{16}"?)

But the output I get looks like E00401007758725D"

I followed this regex link for checking my Expression.

Upvotes: 1

Views: 46

Answers (2)

ikegami
ikegami

Reputation: 386396

If you don't want to capture the ", move "? out of the capture.

[pP]iece[_]*?[iI]*[dD]* ?[=]* ?"?(?<PieceID>[A-Z0-9]{16})"?

Tested


That said, ending with something optional is useless. Also, you have unneeded square brackets, and those stars are suboptimal. Fixed:

[pP]iece(?:_?[iI][dD])?\s*(?:=\s*)?"?(?<PieceID>[A-Z0-9]{16})

Tested

Upvotes: 2

alariva
alariva

Reputation: 2139

This expression matches all your sample lines

Named group

.*[=\ ]\"?(?<pieceId>E.*)\"?

Anonymous group

.*[=\ ]\"?(E.*)\"?

Upvotes: 1

Related Questions