Archna Rangrej
Archna Rangrej

Reputation: 664

I want regular expression for first occurrence in the first line

Here is the text.

H:46"  W:22.75"  D:24.5"
Inside: W:15.5"  D:17"
COM Requirements: 3 yds
COL Requirements: 54 sq ft
Also Available: 73-26 26" Seat Height

I need to find out width for the first occurrence in the first line. I want width 22.75.

I also try out with regex.com

enter image description here

Here i find out two occurrences for width

now I use W:(\d+\.?\d*)\1 for the first occurrence but i can't find it out.

Upvotes: 0

Views: 68

Answers (2)

Bahador Raghibizadeh
Bahador Raghibizadeh

Reputation: 1265

use from lookbehind and lookahead:

/(?<=W:)(\d+\.?\d*)(?=")/g

To be more precise and to avoid errors, it is best to specify the pattern before and after. test: Link

Upvotes: 0

Soc
Soc

Reputation: 7780

Drop the global (g) flag and it will only return the first match. i.e. /W:(\d+\.?\d*)/ instead of /W:(\d+\.?\d*)/g

Upvotes: 1

Related Questions