scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.52k stars 345 forks source link

group(i) returns wrong value #132

Closed grifas closed 2 years ago

grifas commented 4 years ago

When I use matcher.group(1), the value it's not corresponding to the regex. I got the value but some extra characters are present. I think it's about to find the substring from the range.

I replaced the function:

 public func group(_ i: Int) -> String? {
        let b = matches[index]
        #if !os(Linux) && !swift(>=4)
            let c = b.rangeAt(i)
        #else
            let c = b.range(at: i)
        #endif

        if(c.location == NSNotFound) {return nil}
        let result = string.substring(c.location, c.length)
        return result
    }

by:

public func group(_ i: Int) -> String {
        let b = matches[index]
    let result = NSString(string: string).substring(with: b.range)

    return result
    }

and I got the right value.

Maybe Matcher can be initialized with the NSString to avoid redundant instance

scinfu commented 4 years ago

Hi, NSString is not part of Swift . Can you give an example of a wrong code?

Thank you