I am new in swift, I have been working with it only few weeks and now I am trying to parse something like a price list from incoming string. It has the next format:
2.99 X 3.00 = 10 A
Some text here
1.22 X 1.5 10 A
And the hardest part is that sometime A or some digit is missing but X should be in the place.
I would like to find out how it is possible to use regex in swift (or something like that if it does not exist) to write a template for parsing the next value
d.dd X d.d SomeValueIfExists
I would very appreciate any useful information, topics to read or any other resources to get more knowledge about swift.
PS. I have access to the dev. forums but I've never used them before.
I did an example recentl, and maybe a little harder than necessary, to demonstrate RegEx use in Swift:
let str1: NSString = "I run 12 miles"
let str2 = "I run 12 miles"
let match = str1.rangeOfString("\\d+", options: .RegularExpressionSearch)
let finalStr = str1.substringWithRange(match).toInt()
let n: Double = 2.2*Double(finalStr!)
let newStr = str2.stringByReplacingOccurrencesOfString("\\d+", withString: "\(n)", options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
println(newStr) //I run 26.4 miles
Two of these have "RegularExpressionSearch". If you put this in a playground you can see what each line does. Note the double \ escapes. One for the normal RegEx use and anther because \ is a special character in Swift.
Also a good article:
http://benscheirman.com/2014/06/regex-in-swift/
Related
I am new to Spark. I want to output the top 2 twitter mentions using this test.txt file:
"I love to dance #Kelsey, especially with you #Kelsey!"
"Can't believe you went to #harvard. Come on man #harvard"
"I love #harvard"
Essentially, multiple mentions in a single tweet only counts once. So the output would be like:
(2, #harvard)
(1, #Kelsey)
So far, my codes looks like the following:
val tweets = sc.textFile("testFile")
val myReg = """(?<=#)([\\w]+)""".r
val mentions = tweets.filter(x => (myReg.pattern.matcher(x).matches))
However, it would not work because x is still a line and it will not match as a result. Is there anyway I can test the word in the line instead of the line itself? Also, how do I check if that mention is redundant in the tweet?
I adjusted your regex a little and you might need to translate it back to spark syntax, but this way you find all mentions and group them. The .toSet is important to remove duplicates, .toLowercase would also make sense there
val tweets = List("I love to dance #Kelsey, especially with you #Kelsey!",
"Can't believe you went to #harvard. Come on man #harvard",
"I love #harvard")
val myReg = """(#\w+)""".r
val mentions = tweets.flatMap(x => myReg.findAllIn(x).toSet).groupBy(identity).mapValues(_.length)
println(mentions)
That works for me, the regexs is more tweeter exact
val myReg = "(^|[^#\\w])#(\\w{1,15})\\b".r
val mentions = tweets.flatMap(x => myReg.findAllIn(x).matchData.map(_.group(0).trim -> 1)).reduceByKey(_ + _)
I've been reading the Apple Developer Documentation and it appears that it's not updated for the class NumberFormatter, they say it swapped from NSNumberFormatter to just NumberFormatter.
I've found a few examples of functionalities of this class in Swift 3 but I couldn't find how to set the maximumFractionDigits.
When I have a Double like this 0.123456789, I'd like to convert it into a String with just 4 fractional digits for example, like this 0.1234.
If you don't want it to round up, but rather always round down, use .floor or .down:
let foo = 0.123456789
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 4
formatter.roundingMode = .down
let string = formatter.string(from: NSNumber(value: foo))
If you want the traditional rounding format, just omit the .roundingMode, and this will result in "0.1235".
For more information, see the NumberFormatter reference documentation.
I have a CSV file containing user (tweetid, tweets, userid).
396124436476092416,"Think about the life you livin but don't think so hard it hurts Life is truly a gift, but at the same it is a curse",Obey_Jony09
396124436740317184,"“#BleacherReport: Halloween has given us this amazing Derrick Rose photo (via #amandakaschube, #ScottStrazzante) http://t.co/tM0wEugZR1” yes",Colten_stamkos
396124436845178880,"When's 12.4k gonna roll around",Matty_T_03
Now I need to write a Pig Query that returns all the tweets that include the word 'favorite', ordered by tweet id.
For this I have the following code:
A = load '/user/pig/tweets' as (line);
B = FOREACH A GENERATE FLATTEN(REGEX_EXTRACT_ALL(line,'(.*)[,”:-](.*)[“,:-](.*)')) AS (tweetid:long,msg:chararray,userid:chararray);
C = filter B by msg matches '.*favorite.*';
D = order C by tweetid;
How does the regular expression work here in splitting the output in desired way?
I tried using REGEX_EXTRACT instead of REGEX_EXTRACT_ALL as I find that much more simpler, but couldn't get the code working except for extracting just the tweets:
B = FOREACH A GENERATE FLATTEN(REGEX_EXTRACT(line,'[,”:-](.*)[“,:-]',1)) AS (msg:chararray);
the above alias gets me the tweets, but if I use REGEX_EXTRACT to get the tweet_id, I do not get the desired o/p: B = FOREACH A GENERATE FLATTEN(REGEX_EXTRACT(line,'(.*)[,”:-]',1)) AS (tweetid:long);
(396124554353197056,"Just saw #samantha0wen and #DakotaFears at the drake concert #waddup")
(396124554172432384,"#Yutika_Diwadkar I'm just so bright 😁")
(396124554609033216,"#TB23GMODE i don't know, i'm just saying, why you in GA though? that's where you from?")
(396124554805776385,"#MichaelThe_Lion me too 😒")
(396124552540852226,"Happy Halloween from us 2 #maddow & #Rev_AlSharpton :) http://t.co/uC35lDFQYn")
grunt>
Please help.
Can't comment, but from looking at this and testing it out, it looks like your quotes in the regex are different from those in the csv.
" in the csv
” in the regex code.
To get the tweetid try this:
B = FOREACH A GENERATE FLATTEN(REGEX_EXTRACT(line,'.*(,")',1)) AS (tweetid:long);
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. ;)
Is there default(in SDK) scala support for string templating? Example: "$firstName $lastName"(named not numbered parameters) or even constructs like for/if. If there is no such default engine, what is the best scala library to accomplish this.
If you want a templating engine, I suggest you have a look at scalate. If you just need string interpolation, "%s %s".format(firstName, lastName) is your friend.
Complementing Kim's answer, note that Java's Formatter accepts positional parameters. For example:
"%2$s %1$s".format(firstName, lastName)
Also, there's the Enhanced Strings plugin, which allows one to embed arbitrary expressions on Strings. For example:
#EnhanceStrings // enhance strings in this scope
trait Example1 {
val x = 5
val str = "Inner string arithmetics: #{{ x * x + 12 }}"
}
See also this question for more answers, as this is really a close duplicate.
In Scala 2.10 and up, you can use string interpolation
val name = "James"
println(s"Hello, $name") // Hello, James
val height = 1.9d
println(f"$name%s is $height%2.2f meters tall") // James is 1.90 meters tall
This compiler plug-in has provided string interpolation for a while:
http://jrudolph.github.com/scala-enhanced-strings/Overview.scala.html
More recently, the feature seems to be making it into the scala trunk: https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala -- which generates some interesting possiblities: https://gist.github.com/a69d8ffbfe9f42e65fbf (not sure if these were possible with the plug-in; I doubt it).