I am converting a CoreText based app to Swift and I am facing an issue when getting the matches to a regular expression in the text.
This is the sample code
let regexOptions = NSRegularExpressionOptions.CaseInsensitive | NSRegularExpressionOptions.DotMatchesLineSeparators
let regex = NSRegularExpression.regularExpressionWithPattern("(.*?)(<[^>]+>|\\Z)", options: regexOptions, error: nil)
var results: Array<NSTextCheckingResult> = regex.matchesInString(text, options: 0, range: NSMakeRange(0, countElements(text)))
According to the documentation, the matchesInString function returns an array of NSTextCheckingResults, but the compiler complains stating that "The Expression of type anyObject[] canĀ“t be converted to "NSMatchingOptions". Any idea of what might be wrong here?
Try assigning to your results variable like this:
var results = regex.matchesInString(text, options: nil, range: NSMakeRange(0, countElements(text))) as Array<NSTextCheckingResult>
the return type is Array<AnyObject>[]!, you can cast here (as in the above example) or later when you check the members of the collection
in Swift options take nil to represent an empty option set (vs. 0 in Objective-C)
I just sat with a problem related to some regexes and thought I would add a warning to the answer submitted above. My regexes matches seemed to be cut short and it turned out that the range i supplied was incorrect. I generated the range in the way described by #fqdn. It turned out that my strings contained carriage returns (\u{A}) and that these were not counted by the countElements function.
I countered this by calling .unicodeScalars on the string which seems to correctify the lenght.
println(countElements("\u{A}\u{A}\u{A}\n\u{D}\n\u{D}\n\u{D}\n\u{D}\n")) //8
println(countElements("\u{A}\u{A}\u{A}\n\u{D}\n\u{D}\n\u{D}\n\u{D}\n".unicodeScalars)) //12
Disclaimer: This is quite probably a swift-bug and might get fixed in a later version.
Related
I'm brand new to using PBI but as far as I can tell, I should be able to substitute a parameter as part of a Direct Query in place of a hard-coded variable...ie
let
Source = Sql.Database("NAMEOFDB", "CMUtility", [Query="sp_get_residentsinfo "& home_name]),.....
instead of
let
Source = Sql.Database("NAMEOFDB", "CMUtility", [Query="sp_get_residentsinfo 'NAME OF HOME'"]),...
However, the parameter-included version just says
DataSource.Error: Microsoft SQL: Incorrect syntax near 'House'.
Details:
DataSourceKind=SQL
DataSourcePath=NAMEOFDB;CMUtility
Message=Incorrect syntax near 'House'.
Number=102
Class=15
"House" is the currently - assigned last word of the home_name variable. What have I done wrong?
PS - I have surmised that I shouldn't need the extra & at the end of the parameter, as I'm not adding anything else to the query, but even with both &s it still doesn't work.
The type of your parameters is text. In SQL, text literals must be quoted, i.e. sp_get_residentsinfo 'NAME OF HOME', but the statement build by you is sp_get_residentsinfo NAME OF HOME.
You should use Text.Replace to escape single quotes in the parameter's value and append a quote before and after it.
I'm doing an introductory iOS developer's course, and am doing a cat-years calculator as an exercise. The following code returns the error:
Int(AgeInCatYears.text!) = CatsROld!
Error - Cannot assign to value: function call returns immutable value
In case it's important, the var CatsROld was set equal to Int(humanYears.text!)! * 7 one line earlier.
Any advice?
If you are setting value to the Label or textField then you need to simply set value (as String) to its property text, but you are try to convert the value to Int and then you assigning value of CatsROld to it, that is the reason you are getting this error. So simply write like AgeInCatYears.text = "\(CatsROld!)"
AgeInCatYears.text = "\(CatsROld!)"
Note: You are force wrapping CatsROld value with !, if you are 100% sure that it will not nil then its ok other wise you need to use if let or guard for optional wrapping.
I have a variable $yearMonth := "2015-02"
I have to search this date on an element Date as xs:dateTime.
I want to use regex expression to find all files/documents having this date "2015-02-??"
I have path-range-index enabled on ModifiedInfo/Date
I am using following code but getting Invalid cast error
let $result := cts:value-match(cts:path-reference("ModifiedInfo/Date"), xs:dateTime("2015-02-??T??:??:??.????"))
I have also used following code and getting same error
let $result := cts:value-match(cts:path-reference("ModifiedInfo/Date"), xs:dateTime(xs:date("2015-02-??"),xs:time("??:??:??.????")))
Kindly help :)
It seems you are trying to use wild card search on Path Range index which has data type xs:dateTime().
But, currently MarkLogic don't support this functionality. There are multiple ways to handle this scenario:
You may create Field index.
You may change it to string index which supports wildcard search.
You may run this workaround to support your existing system:
for $x in cts:values(cts:path-reference("ModifiedInfo/Date"))
return if(starts-with(xs:string($x), '2015-02')) then $x else ()
This query will fetch out values from lexicon and then you may filter your desired date.
You can solve this by combining a couple cts:element-range-querys inside of an and-query:
let $target := "2015-02"
let $low := xs:date($target || "-01")
let $high := $low + xs:yearMonthDuration("P1M")
return
cts:search(
fn:doc(),
cts:and-query((
cts:element-range-query("country", ">=", $low),
cts:element-range-query("country", "<", $high)
))
)
From the cts:element-range-query documentation:
If you want to constrain on a range of values, you can combine multiple cts:element-range-query constructors together with cts:and-query or any of the other composable cts:query constructors, as in the last part of the example below.
You could also consider doing a cts:values with a cts:query param that searches for values between for instance 2015-02-01 and 2015-03-01. Mind though, if multiple dates occur within one document, you will need to post filter manually after all (like in option 3 of Navin), but it could potentially speed up post-filtering a lot..
HTH!
Scala noob i'm afraid:
I have the following declared class variable which will the objects I read from the database:
val options = mutable.LinkedList[DivisionSelectOption]()
I then use JPA to get a List of all rows from a table:
val divisionOptions = em.createNamedQuery("SelectOption.all", classOf[SelectOption]) getResultList
/* Wrap java List in Scala List */
val wrappedOptions = JListWrapper.apply(divisionOptions)
/* Store the wrappedOptions in the class variable */
options += wrappedOptions
However, the last line has an error:
Type Expected: String, actual JListWrapper[SelectOption]
Can anyone help with what I am doing wrong? I'm just trying to populate the options object with the result of the DB call.
Thanks
What (probably) is happening is that a JlistWrapper[SelectOption] isn't a DivisionSelectOption, so the method += isn't applicable to it. That being the case, it is trying other stuff, and giving a final error on this:
options = options + wrappedOptions
That is a rewriting Scala can do to make things like x += 1 work for var x. The + method is present on all objects, but it takes a String as parameter -- that's so one can write stuff like options + ":" and have that work as in Java. But since wrappedOptions isn't a String, it complains.
Roundabout and confusing, I know, and even Odersky regrets his decision with regards to +. Let that be a lesson: if you thing of adding a method to Any, think really hard before doing it.
I have been messing with this for a while now, and decided to post on here to see if anyone could help out. I even messed around with the RegExr tool (with no luck):
http://gskinner.com/RegExr
Anyway, I have a String that contains the verbiage (without the quotes):
"13.5 to 14.1"
I need to create a var with the first number: 13.5 and a var with the second number: 14.1
So I want the following result:
var firstVal:String = 13.5;
var secondVal:String = 14.1;
I got it to work by doing the following for the first number:
var lowRegExp:RegExp=/\d[0-9].\d[0-9]/;
And for the second number I did this:
var highRegExp:RegExp=/\d[0-9].\d[0-9]$/;
My problem here is that I will not know the format of the String. It could also look like this (two digits trailing the decimal):
13.57 to 14.10
So I need to make sure that it works using the following combinations:
13.50 to 14.1, 13.5 to 14.10, 3.50 to 4.10, 3.5 to 4.1 (all combinations must work)
Any help is much appreciated!
Here is what I got to work. I am not sure how clean this is, and I am not a fan of hard coding, but it works for all scenarios. If someone knows a clean way to do this, please let me know.
var myString:String="13.5 to 14.1";
var firstVal:String=myString.substring(0, myString.search(" to "));
var secondVal:String=myString.substring((myString.search(" to ") + 4));
Should be pretty straight forward, you want the following:
- Any # of digits, followed by a period literal, followed by any # of digits.
Pattern: \d+\.\d+
So use something similar:
var mystr:String = "15.4 to 153.93";
var tokens:Array = mystr.match(/\d+\.\d+/g);
Also, I have gotten myself in the habit of using regexpal.com which is way faster than iterative testing in your application. ;)