Regular Expressions in Swift?


(Ethin Probst) #1

Hello all,
This may be a very weird question. However, please bare with me while
I explain this.
I was speaking with a friend over Skype (we're both visually impaired,
so use Windows for pretty much everything) and my friend asked me to
develop a program that can take the numbers of vehicles (he's a GTA
fan, :)) in KMH (kilometers per hour) and convert it into MPH (miles
per hour). To do this, I chose Swift for this task, as I think it
might be a good challenge for it. I am using RemObjects Elements to
use Swift on .NET, Java, Cocoa, and Island to develop applications for
these platforms. Island is a new platform in RemObjects Elements that
allows developers to develop applications that use native API
functions from the operating system. For instance, on Windows, I could
directly (without any imports) use CreateWindowEx and all that to
create a new window. Sadly this cannot be ported to Mac OS X, as Cocoa
is reserved for that. :slight_smile:
Anyways, back to the topic I'm trying to resolve. My friend gave me
too files, specs_cars.txt and specs_motorcycles.txt. Both files are
attached to this email (specs_cars.txt is nearly 45 KB in length and
specs_motorcycles.txt is about 3.9 KB).
So here's my question: I'm on .NET, so yeah, but how would I go about
using regular expressions to only search with lines that contain KMH
(I'll show you how I want it to be done) and replace the numbers of
KMH, along with the KMH itself, with MPH numbers followed by MPH? If
you guys could help me solve this, I would be grateful, as I don't
want to develop an entire parser for something like this, so regular
expressions seem to be the way to go.

Allow me to show you: In the specs_motorcycles.txt file, this:
0-100kmh: 3.9s
Should be replaced with:
0-62.14kmh: 3.9s

Now, this looks simple, but what about something like:
Top speed: 218 kmh
to be replaced with:
Top speed: 135.5 kmh
I hope you see my problem. I hope you can help me through this, and we
can solve it together. :slight_smile:

specs_cars.txt (42.6 KB)

specs_motorcycles.txt (3.91 KB)

···

--
Signed,
Ethin D. Probst


(Zhao Xin) #2

Here is the link you need.
https://developer.apple.com/reference/foundation/nsregularexpression#

Zhaoxin

···

On Sun, Jan 8, 2017 at 2:48 AM, Ethin Probst via swift-users < swift-users@swift.org> wrote:

Hello all,
This may be a very weird question. However, please bare with me while
I explain this.
I was speaking with a friend over Skype (we're both visually impaired,
so use Windows for pretty much everything) and my friend asked me to
develop a program that can take the numbers of vehicles (he's a GTA
fan, :)) in KMH (kilometers per hour) and convert it into MPH (miles
per hour). To do this, I chose Swift for this task, as I think it
might be a good challenge for it. I am using RemObjects Elements to
use Swift on .NET, Java, Cocoa, and Island to develop applications for
these platforms. Island is a new platform in RemObjects Elements that
allows developers to develop applications that use native API
functions from the operating system. For instance, on Windows, I could
directly (without any imports) use CreateWindowEx and all that to
create a new window. Sadly this cannot be ported to Mac OS X, as Cocoa
is reserved for that. :slight_smile:
Anyways, back to the topic I'm trying to resolve. My friend gave me
too files, specs_cars.txt and specs_motorcycles.txt. Both files are
attached to this email (specs_cars.txt is nearly 45 KB in length and
specs_motorcycles.txt is about 3.9 KB).
So here's my question: I'm on .NET, so yeah, but how would I go about
using regular expressions to only search with lines that contain KMH
(I'll show you how I want it to be done) and replace the numbers of
KMH, along with the KMH itself, with MPH numbers followed by MPH? If
you guys could help me solve this, I would be grateful, as I don't
want to develop an entire parser for something like this, so regular
expressions seem to be the way to go.

Allow me to show you: In the specs_motorcycles.txt file, this:
0-100kmh: 3.9s
Should be replaced with:
0-62.14kmh: 3.9s

Now, this looks simple, but what about something like:
Top speed: 218 kmh
to be replaced with:
Top speed: 135.5 kmh
I hope you see my problem. I hope you can help me through this, and we
can solve it together. :slight_smile:

--
Signed,
Ethin D. Probst

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users