Kevin Pimentel
Kevin Pimentel

Reputation: 1916

What is the right RegEx pattern to match this case?

I am trying, and failing miserably, to write regex that will match all of the following lines that have integers in em. I'll add comments to indicate which ones should match.

"modelstub": "some-123908", // No match
"modelstub": "00432", // No match
"modelstub": "1607", // No match
"modelstub": 16-02, // No Match 
"modelstub": 1605, // Match!
"modelstub": 1604, // Match!
"modelstub": 1603, // Match!
"modelstub": "1-602", // No match

Please forgive me for I am using ColdFusion. Trust me no one hates it more than me.

But here's what I tried: ("modelstub":+)[0-9]+

Full code reference:

<cfset output = REReplace(output, '("modelstub":+)[0-9]+', '"modelstub": "$1"', "ALL")>

Upvotes: 1

Views: 37

Answers (2)

The fourth bird
The fourth bird

Reputation: 163632

In the pattern that you use, you are repeating the : 1 or more times using :+ and there is a space missing between the colon and the double quote.

You could update your pattern to ("modelstub":) [0-9]+$ and you will then have a capturing group.

To get the match only without the group, if there is a comma at the end of the string, you could use:

"modelstub": \d+,

Regex demo

If here is no comma, you might use:

"modelstub": \d+$

Regex demo

Upvotes: 2

mrben522
mrben522

Reputation: 417

if there's a comma at the end of each line then the solution from @Thefourthbird will work. if not then try this: "modelstub": \d+$

Upvotes: 1

Related Questions