Removing wikitext hyperlinks in Swift - ios

I'm trying to remove Wikitext hyperlink formatting and just get the link text, but everything I've tried either deletes nothing or deletes much more than expected. Here's the general pattern:
[[user interface]] → user interface
[[Telephone call|calls]] → calls
[[camera phone|take pictures]] → take pictures
If the text between [[ and ]] contains a |, I try to remove the text between [[ and |, remove the closing brackets, but retain the text in-between | and ]]. If it doesn't contain a |, I simply want to remove the brackets, retaining the text between them.
Example:
The iPhone has a [[user interface]] built around a [[multi-touch]] screen. It connects to [[cellular network]]s or [[Wi-Fi]], and can make [[Telephone call|calls]], [[web browser|browse the web]], [[camera phone|take pictures]], [[portable media player|play music]] and send and receive [[email]]s and [[text messaging|text messages]].
Expected result:
The iPhone has a user interface built around a multi-touch screen. It connects to cellular networks or Wi-Fi, and can make calls, browse the web, take pictures, play music and send and receive emails and text messages.
I discovered this answer but upon plugging it into my app, it removed all of the text between [[ and ]], instead of the text between [[ and |.
Using the example above, this is what the code from that answer results in:
The iPhone has a {removed} built around a {removed} screen. It connects to {removed}s or {removed}, and can make {removed}, {removed}, {removed}, {removed} and send and receive {removed}s and {removed}.
This is really leaving me stumped, can anyone help? Thanks!
extension String {
func replacingOccurrencesHyperlinks() -> String {
let regExpr = "\\[\\[[^\\]]+?\\|(.+?)\\]\\]"
return replacingOccurrences(of: regExpr, with: "{removed}", options: .regularExpression)
}
} // this is the extension I use on the MediaWiki API result.

there is probably a better one liner regex solution, but I forgot how it works, in the meantime try this to print the Expected result:
func replacingOccurrencesHyperlinks() -> String {
return self.replacingOccurrences(of: "\\[\\[(?:[^\\]|]*\\|)", with: "", options: .regularExpression)
.replacingOccurrences(of: "[[", with: "")
.replacingOccurrences(of: "]]", with: "")
}

Resolved by an awesome guy over on Reddit. Just needed to replace with: "{removed}" in the extension with simply with: "$1"!

Related

Bookmarks parsing issue

I have a LARGE number of bookmarks and wanted to export them and share them with a group I work with. The issue is that when I export them, there are ADD_DATE and LAST_MODIFIED fields added by the browser (Firefox). I was hoping to just use cut or awk to pull the fields I want but the lack of a space before the >(website_name) is making that difficult. And my regex skills are weak.
How do I add a single space before the second to last > at the end of the line so that I can use cut or awk to pull out the fields I want into a new file?
Ex: 123456">SecurityTrails would become 123456 >SecurityTrails
Please see below for examples of what I'm working with. Any help is greatly appreciated!
<DT>SecurityTrails
i use firefox myself. it frequently also embeds favicon into the exported bookmarks.html file via base64 encoding. so to account for the different scenarios (than just the one mentioned by OP), maybe something like
{mawk/mawk2/gawk} 'BEGIN { FS = "\042" } $1 = $1'
then do whatever cutting that you want. That's just assuming OP wanted to keep every bit of it, and simply remove the quotations.
Now, if the objective is just to take out URL+Name of it,
{mawk/mawk2/gawk} 'BEGIN { DBLQT="\042"; FS = "(<A HREF=" DBLQT "|>)" } /<A HREF=/ {
url = substr($2, 1, index($2, DBLQT) - 1);
sitename = $(NF-1);
sub(/<\/A$/, "", sitename) ;
print url " > " sitename ; }' # or whatever way you want the output to be
I just typed it in extra verbosity to show what \042 meant - the ascii octal for double quote.

Lua: Find string between two strings

local code = [[
print("this is a trap")
asd("XDasdsadasdasd")
print("this is a trap")
]]
print("\n\n\n\n\n")
print(string.match(code, 'asd(.*)'))
I made this but the problem is it also returns the print under it. It'll return anything under the asd("XDasdsadasdasd"), but i only want whats inside asd("XDasdsadasdasd") to return that will be "XDasdsadasdasd"
Parentheses are magic chars in Lua patterns. You need to escape them. Also, you need to stop at the first closing parenthesis. See the code below:
print(string.match(code, 'asd%(.-%)'))
If you only want what's inside asd(...), then use
print(string.match(code, 'asd%((.-)%)'))

Xcode is throwing me an error in swift

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \
(myPlacement.country) \
(myPlacement.postalCode)"
}
. But when I try I get an error
unterminated string literal
and
consecutive statements on a line must be seperated by a ';'
but the guy in the tutorial has done it the exact same way. What's going on?
I'm using the latest swift and and latest xcode 7.2 any help would be apreciated
if I write everything on the same line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
}
it works fine though
if I write everything on the same line like this
Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:
let s = "hello
there"
There is no magic line-continuation character which, placed at the end of the first line, would make that legal.
If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.
You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:
let myAddress = "\(myPlacement.locality) " +
"\(myPlacement.country) " +
"\(myPlacement.postalCode)"
I look your video tutorial carefully. You have a misunderstanding here.
You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.
So, the real code is
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
Please watch it carefully.
And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

readByteSync - is this behavior correct?

stdin.readByteSync has recently been added to Dart.
Using stdin.readByteSync for data entry, I am attempting to allow a default value and if an entry is made by the operator, to clear the default value. If no entry is made and just enter is pressed, then the default is used.
What appears to be happening however is that no terminal output is sent to the terminal until a newline character is entered. Therefore when I do a print() or a stdout.write(), it is delayed until newline is entered.
Therefore, when operator enters first character to override default, the default is not cleared. IE. The default is "abc", data entered is "xx", however "xxc" is showing on screen after entry of "xx". The "problem" appears to be that no "writes" to the terminal are sent until newline is entered.
While I can find an alternative way of doing this, I would like to know if this is the way readByteSync should or must work. If so, I’ll find an alternative way of doing what I want.
// Example program //
import 'dart:io';
void main () {
int iInput;
List<int> lCharCodes = [];
print(""); print("");
String sDefault = "abc";
stdout.write ("Enter data : $sDefault\b\b\b");
while (iInput != 10) { // wait for newline
iInput = stdin.readByteSync();
if (iInput == 8 && lCharCodes.length > 0) { // bs
lCharCodes.removeLast();
} else if (iInput > 31) { // ascii printable char
lCharCodes.add(iInput);
if (lCharCodes.length == 1)
stdout.write (" \b\b\b\b chars cleared"); // clear line
print ("\nlCharCodes length = ${lCharCodes.length}");
}
}
print ("\nData entered = ${new String.fromCharCodes(lCharCodes).trim()}");
}
Results on Command screen are :
c:\Users\Brian\dart-dev1\test\bin>dart testsync001.dart
Enter data : xxc
chars cleared
lCharCodes length = 1
lCharCodes length = 2
Data entered = xx
c:\Users\Brian\dart-dev1\test\bin>
I recently added stdin.readByteSync and readLineSync, to easier create small scrips reading the stdin. However, two things are still missing, for this to be feature-complete.
1) Line mode vs Raw mode. This is basically what you are asking for, a way to get a char as soon as it's printed.
2) Echo on/off. This mode is useful for e.g. typing in passwords, so you can disable the default echo of the characters.
I hope to be able to implement and land these features rather soon.
You can star this bug to track the development of it!
This is common behavior for consoles. Try to flush the output with stdout.flush().
Edit: my mistake. I looked at a very old revision (dartlang-test). The current API does not provide any means to flush stdout. Feel free to file a bug.

How do I know whether I'm looking at a newline or carriage return etc.?

For example, say I wanted to determine whether this form was storing newlines as carriage returns or newlines or whatever characters. I'm often in situations where I'm writing code and am not sure what type of new-line character a file/form/whatever I'm parsing is using.
How could I determine this? Is there a way to determine this without actually doing a check inside of code? (It seems like I should be able to right-click and "show all characters" or something like that).
Note: I realize I could write code saying
(if == '\r') cout << "Carriage";
etc
but I have a feeling there's a simpler solution.
Maybe is list what you are looking for (from vim help):
:[range]l[ist] [count] [flags]
Same as :print, but display unprintable characters
with '^' and put $ after the line. This can be
changed with the 'listchars' option.
See ex-flags for [flags].
You can switch modes with:
:set list
and
:set nolist
Additionally you can use "listchars" as shown in this example:
You could for example check your document for occourences of "Carriage Return" or "New Line"/"Line Feed".
e.g. (php):
if( strstr( $yourstring , "\r" ) != false ){ // You have Carriage return
// Do something
}
elseif( strstr( $yourstring , "\n" ) != false ){ // You have New Line/Line feed
// Do something
}
else{
// You cannot determine which on is used, because the string is single-lined
}
I hope this is the thing you're looking for
Note: In windows "\r\n" is used to specify ne lines

Resources