0

I have a big string that contains several strings like the following:

http://website.com/image.jpg and I want to add ?w=100 to each of them.

So far I thought about creating a regular expression where every time I would encounter that string, it would replace it by the same string with the ?w=100 added to it.

Here is an extension of String to replace the string:

public func stringByReplacingMatches(matchRegEx: String, withMatch: String) -> String {
    do {
        let regEx = try NSRegularExpression(pattern: matchRegEx, options: NSRegularExpressionOptions())
        return regEx.stringByReplacingMatchesInString(self, options: NSMatchingOptions(), range: NSMakeRange(0, self.characters.count), withTemplate: withMatch)
    } catch {
        fatalError("Error in the regular expression: \"\(matchRegEx)\"")
    }
}

And this is how I use it:

myString.stringByReplacingMatches("https:\\/\\/website\\.com\\/[\\w\\D]*.jpg", withMatch: "https:\\/\\/website\\.com\\/[\\w\\D]*.jpg?w=100"))

Problem, this is what I get after replacing the string:

https://website.com/[wD]*.jpg?w=100

How can I solve my problem?

3 Answers 3

1

You need to learn about capture groups:

myString.stringByReplacingMatches("(https:\\/\\/website\\.com\\/[\\w\\D]*.jpg)", withMatch: "$1?w=100")

Oh and don't forget to save the return value

let fixedString = myString.stringByReplacingMatches("(https:\\/\\/website\\.com\\/[\\w\\D]*.jpg)", withMatch: "$1?w=100")

myString will not change, but fixedString will have all the updates.


UPDATE

[\\w\\D]* matches any character. You need to craft a more specific regular expression.

For the input "https://website.com/x.jpg" "https://website.com/x.jpg", the regex \w works.

var myString = "\"https://website.com/x.jpg\" \"https://website.com/x.jpg\""
myString = myString.stringByReplacingMatches("(https:\\/\\/website\\.com\\/\\w*.jpg)", withMatch: "$1?w=100")

But this will not work for every possible URL.

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

5 Comments

Why do you need a capture group if you can use $0 to insert the matched string?
@paulvs I prefer to be explicit. $0 works, but is less flexible.
It works thanks but only for the last occurrence. How can I make it work for all the occurrences in my big string? I guess the regex is fine, it's probably the function.
@Nico \w matches any word, \D matches any non-digit. Between the two, it matches every character. You need a better regular expression.
Yes I realised it afterwards... I ended up using this regex: [\\w-$+()*!']*. Might need to get a better one though. Thanks
1

You should call it as

myString.stringByReplacingMatches("https:\\/\\/website\\.com\\/[\\w\\D]*.jpg", withMatch: "$0?w=100"))

The template can use $0 to show the matching string. You should not repeat the matching regular expression there.

3 Comments

It works thanks but only for the last occurrence. How can I make it work for all the occurrences in my big string? I guess the regex is fine, it's probably the function.
Your matchign expression is too broad. Try myString.stringByReplacingMatches("http:\\/\\/[a-zA-Z0-9]*.[a-zA-Z0-9]*\\/[a-zA-Z0-9]*.[a-zA-Z0-9]*", withMatch: "$0?w=100")) That may work from some URLs, but I would look for a better regular expression to detect all different types of URLs. For your original question, the template change is the correct solution. The correct matching of URLs in a larger string is a different question altogether.
Yes I realised it afterwards... I ended up using this regex: [\\w-$+()*!']*. Might need to get a better one though. Thanks
0
do {
    let regex = try NSRegularExpression(pattern: "http:\\/\\/[a-z]*.[a-z]*\\/[a-z]*.[a-z]*", options: .CaseInsensitive)
    var newString = regex.stringByReplacingMatchesInString(a, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, a.characters.count), withTemplate: "$0?w=100")
    print(newString)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.