1

Given this string:

if let value = json["PropertyID"].int { self.propertyID = value } 

I have successfully matched and replaced to this one:

self.propertyID = representation.valueForKey("PropertyID")?.integerValue 

Note: the entire string changes, except for propertyID and "PropertyID" that are reused to build the new string

And so, applying this regex expression:

// Match if let value = json\["([^\)]*)"\].int \{ self.([^\)]*) = value \} // Replace self.$2 = representation.valueForKey("$1")?.integerValue 

The result output is this perfectly formatted string:

self.propertyID = representation.valueForKey("PropertyID")?.integerValue 

So far so good! However, the text/code file that I'm trying to match and replace, actually looks more like this:

if let value = json["PropertyID"].int { self.propertyID = value } if let value = json["UniqueID"].string { self.propertyUniqueID = value } if let value = json["TypologyDescription"].string { self.typology = value } if let value = json["RoomsNumber"].int { self.bedrooms = value } if let value = json["BathroomsNumber"].int { self.bathrooms = value } if let value = json["GrossBuiltArea"].decimal { self.grossBuiltArea = value } if let value = json["TotalLandArea"].decimal { self.totalLandArea = value } if let value = json["CurrentAskingPrice"].decimal { self.price = value } if let value = json["CurrencyDescription"].string { self.currency = value } if let value = json["CountryDescription"].string { self.country = value } if let value = json["DistrictDescription"].string { self.district = value } if let value = json["CountyDescription"].string { self.county = value } if let value = json["ParishDescription"].string { self.parish = value } if let value = json["Latitude"].decimal { self.latitude = value } if let value = json["Longitude"].decimal { self.longitude = value } if let value = json["DefaultPhotoID"].int { self.defaultPhotoID = value } if let value = json["PropertyStageStatus"].string { self.propertyStageStatusDescription = value } if let value = json["PropertyStageSubStatus"].string { self.propertyStageSubStatusDescription = value } 

At this point, the regex expression doesn't seem to care about boundaries. It matches the entire text instead of multiple occurrences (I'm expecting 4 in this example).

I understand why this happens! Just not how to overcome it yet... I need it to begin at "if" and end at "}" and don't go beyond that line! Giving me the 4 occurrences and not the whole text/code as 1 single occurrence.

I've been trying to understand boundaries and anchors but unable to apply them to this context so far.

Thank you!

2 Answers 2

1

You are doing it wrong. This is correct regex

json\["([^\]]*)"\]\.int \{ self\.([^}]*) = value \} 

Regex Demo

Mistakes in your regex

i) . represent any character. You need to escape . as \. to match . literally

ii) You are using \["([^\)]*)"\] but you need to use ["([^]]*)"\] because you want to match anything except ] not ). You can even use ["([^"]*)"\]

iii) You are using ([^\)]*) but you need to use ([^}]*) or ([^\s]*) or ([^=]*)

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is in the regular expression. The regex you used is:

if let value = json\["([^\)]*)"\] ...... 

What does that bit on the right actually mean? Here is a visualisation.

Why are you looping over "anything but a closing bracket"? This is what's causing your match to go across multiple lines.

Instead, what you should be using is "anything but a " character", i.e.

if let value = json\["([^"]*)"\] ...... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.