Search for a Regular Expression in Swift 3 - regex

At the moment I'm converting my project to Swift 3.
I have a code block like this:
let someString = "asd.asABCDEFG.HI"
let regexp = "^\\w*[.]\\w{2}"
let range = someString.rangeOfString(regexp, options: .RegularExpressionSearch)
let result = someString.substringWithRange(range!)
The rangeOfString method is gone in Swift 3. Can somebody post an example, how a regexp search can be done. Thanks in advance.

In Swift 3 rangeOfString is like range(of:options:) and substringWithRange is like substring(with:).
if let range = someString.range(of:regexp, options: .regularExpression) {
let result = someString.substring(with:range)
}

Related

input format specific regex

i want that my input text repect this format :
start with P0E and 10 digits:
P0E0532313118
how i can do it with jquery or java script please ?
thanks,
Pure JS:
let value = 'P0E0532313118';
let isValid = /^(P0E[0-9]{10})$/.test(value); // = TRUE

How to set maximumFractionDigits in swift 3 with NumberFormatter()

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.

Regex in swift. A template for a specific numeric format

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/

Get Tridion Item ID with from URI Regex

If I have a Tridion URI like this 'tcm:1-23-8' and I want to get 23 with a Regular Expression.
The following works, but I know there is a better way. tcm: and '-8' are always there. The parts that change are 1 and 23.
var schemaUri = $display.getItem().getId(); // tcm:1-23-8
var re = /tcm:\d-/gi; // = 23-8
var schemaIdWithItemType = schemaUri.replace(re, "");
re = /-8/gi;
var schemaId = schemaIdWithItemType.replace(re, "");
If the number is always between the 2 dashes, you could do this:
var schemaId = schemaUri.split('-')[1];
This does the following:
split the string on the '-' character --> ['tcm:1', '23', '8'];
Get the second item from that array, '23'
Or, try this:
var schemaId = schemaUri.match(/-\d+-/)[0].replace(/-/g,'');
This'll find the number in between the dashes with .match(/-\d+-/), then remove the dashes.
Rather than calling $display.getItem().getId();, you can just call $display.getUri(); and then use the split()
var schemaId = $display.getUri().split('-')[1];
If you did want a pure Regex solution...
/^tcm:(\d+)-(\d+)(?:-(\d+))?$/i
Should validate your Tridion URI's format and provide you with 3 submatches, the second of which will be the Item ID

NSString simple pattern matching

Mac OS 10.6, Cocoa project, 10.4 compatibility required.
(Please note: my knowledge of regex is quite slight)
I need to parse NSStrings, for matching cases where the string contains an embedded tag, where the tag format is:
[xxxx]
Where xxxx are random characters.
e.g. "The quick brown [foxy] fox likes sox".
In the above case, I need to grab the string "foxy". (Or nil if no tag is found.)
Each string will only have one tag, and the tag can appear anywhere within the string, or may not appear at all.
Could someone please help with a way to do that, preferably without having to include another library such as RegexKit. Thank you for any help.
I'd suggest something like the following:
NSString *subString = nil;
NSRange range1 = [myString rangeOfString:#"["];
NSRange range2 = [myString rangeOfString:#"]"];
if ((range1.length == 1) && (range2.length == 1) && (range2.location > range1.location)) {
NSRange range3;
range3.location = range1.location+1;
range3.length = (range2.location - range1.location)-1;
subString = [myString substringWithRange:range3];
}