-4

Hi am trying to get values of a string which is imputed in the form "40|LQ,FP,MD,GR \"Dinner out\"". The string value I am trying to extract is Dinner out the text could be different though like ride in but it still follows the same pattern. how can I extract this string value from the rest of the character using regex of any alternative.

King
  • 1,885
  • 3
  • 27
  • 84

2 Answers2

-1

You can try

var text =  "40|LQ,FP,MD,GR \"Dinner out\""

var regex = try? NSRegularExpression(pattern: "\\\".+?\\\"", options: [])

var matches = regex?.matches(in: text, options: []
    , range: NSMakeRange(0, text.count)) ?? []

for match in matches {
    let r = (text as NSString).substring(with: match.range)
    print("found= \(r)")
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
-1

I would recommend you to have a look at this question and its answers.

Swift Get string between 2 strings in a string

Quoting answer here just for your ease.

extension String {

func slice(from: String, to: String) -> String? {
        return (range(of: from)?.upperBound).flatMap { substringFrom in
            (range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in
                String(self[substringFrom..<substringTo])
            }
        }
    }
}
Ganesh Somani
  • 2,280
  • 2
  • 28
  • 37