-3

I've a string having 'Optional(someVal)' in it. I want a regular expression to find and replace all occurrences of this keyword and replace just inside values in it. What I'm doing is...

query = "Insert Into table (Col, Col, Col, Col, Col, Col4, Col4, Col4, Col, AuditStatusId, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col, Col ) Values ('71E3F86A059A40F0B3F7614E08ACD2B9', 1, Optional(4), Optional(6), ' ', '', Optional(40799), 'NULL', NULL, 1, 1, NULL, NULL, NUll, 0, NULL, 1, '', NULL , NULL, 90.0, 100.0, 70.0, 89.99, 0.0, 69.99, 1, 1, '2015-01-15 14:09:34 +0000', 0 )"

and replacing using regular expression,

 query = query.stringByReplacingOccurrencesOfString("(\\Optional(w+))", withString: "$1", options: NSStringCompareOptions.RegularExpressionSearch, range: Range<String.Index> (start: query.startIndex, end: query.endIndex)) 

But this is not working even in Objective C. Anybody has idea how to do that??

EDIT: I've tried to overcome this by unwrapping values but it didn't come up with the solution what I'm looking for. What I need is to keep values as 'nil' in case no value and use value inside 'Optional(val)' in case value is entered. So please avoid suggesting Unwrapping for values.

Saad
  • 8,857
  • 2
  • 41
  • 51
  • 6
    That is most probably the wrong approach. You should unwrap the optionals *before* creating the query string. – Martin R Jan 15 '15 at 14:35
  • 2
    And I do hope that you use prepared statements !? (http://xkcd.com/327/) – Martin R Jan 15 '15 at 14:36
  • I'm using '!' instead of '?' and if the value is nil. It writes down a nil which I'm replacing with NULL. Moreover, if values are 20, you mean to put 20 if's. Wouldn't that might be the wrong approach? – Saad Jan 15 '15 at 14:37
  • 2
    Wouldn't if be easier to unwrap the values? Use coalescing operator when value is needed. `optionalValue ?? defaultValue`. – Kirsteins Jan 15 '15 at 14:37
  • Nah! I need as exactly as values passed. 'Nil' In case no value which is working and the entered Value which is appearing as 'Optional(val)'. Which is the issue. I've been done with testing this with unwrapping and it didn't come up with solution to my requirement. – Saad Jan 15 '15 at 14:40
  • I don't know why people are down Voting the question. If you don't know the answer, just go, why don't you express it like you'r a looser? or put your comment here for down voting at least – Saad Jan 15 '15 at 14:48
  • @Kirsteins: Only saw your comment after writing the answer. Your suggestion is much easier. Consider to post an answer, then I will delete mine. – Martin R Jan 15 '15 at 14:55

2 Answers2

5

I know I am not answering your original question, but your approach promotes bad practice. Its better to unwrap the values before inserting them into a query. For example,

var optional1: Int? = 10
var optional2: Int?

optional1?.description ?? "NULL" // "10"
optional2?.description ?? "NULL" // "NULL"
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
2

I found answer to my self. As all of your comments and answers showed only unwrapping values but if I've 30 Columns, then I'll have to put 30 checks or 30 statements having optional1?.description ?? "NULL". While I can just replace nil with NULL and Optional(value) with value with a single statement of regular expression as:

in Swift:

query = query.stringByReplacingOccurrencesOfString("\\Optional[(](\\w+)[)]", withString: "$1", options: NSStringCompareOptions.RegularExpressionSearch, range: Range<String.Index> (start: query.startIndex, end: query.endIndex))

and in objective C:

query = [query stringByReplacingOccurrencesOfString:@"\\Optional[(](\\w+)[)]"
                                         withString:@"222"
                                            options:NSRegularExpressionSearch
                                              range:NSMakeRange(0, [query length])];
Saad
  • 8,857
  • 2
  • 41
  • 51