Weird Characters in Python when reading a file's name - printing

Code:
path5 = '2.Project\WP101 (237641784)\QR2\5.Project\\'
print ('path5 =',path5)
I get:
path5 = 2.Project\WP101 (237641784)\QR2.Project\
What can I do to stop getting the weird sign after QR2 in the path name?

You need to ignore escape chars.
#ignoring escape sequences
#ignoring single quote escape sequences
str1 = r"Hi, I\'m IncludeHelp"
#ignoring double quotes escape sequences
str2 = r"\"Hello world\""
#ignoring path escape sequences
str3 = r"D:\\work_folder\\python_works"
#ignoring hexadecimal values escape sequences
str4 = r"This is \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70"
print(str1);
print(str2);
print(str3);
print(str4);
Just put an r infront of your string and you are good to go.
In your case:
path5 = r'2.Project\WP101 (237641784)\QR2\5.Project\'
print ('path5 =',path5)

Related

Regex to not allow staring whitespace in string swift ios

I want to create regex thats allows characters numbers and spaces but not at the begning of string , i have created below one but its not working "^\\S.*[^A-Za-z0-9_ ].*".
Swift:
func containsAllowedCharacters(regex: String?, stringToCheck: String) -> Bool {
var isValid = false
if let regex = regex {
let testString = NSPredicate(format: "SELF MATCHES %#", regex)
isValid = testString.evaluate(with: stringToCheck)
}
return !isValid
}
Your pattern, ^\S.*[^A-Za-z0-9_ ].*, matches start of string with ^, then matches any non-whitespace char with \S (note it matches any punctuation, letters and digits), then matches any zero or more chars other than line break chars as many as possbile with .*, then matches any char other than an ASCII letter, digit, _ or space, and then again matches any zero or more chars other than line break chars as many as possbile with .*.
As you see, all the pattern parts match more than you allow. Also, pay attention you actually require at least two chars to be present with this pattern while your code implies you need to support zero length string, too.
You can use
let FileNameRegex = #"^(?!\s)[A-Za-z0-9_\s]*$"#
NOTE: As you are using it with MATCHES in the NSPredicate.evaluate, you do not need the ^ and $ anchors on both ends since MATCHES requires a full string match:
let FileNameRegex = #"(?!\s)[A-Za-z0-9_\s]*"#
let testString = NSPredicate(format: "SELF MATCHES %#", regex)
Note the use of #"..."# notation that makes a raw string literal, where a single backslash is parsed as a literal backslash.
The pattern matches
^ - start of string
(?!\s) - a negative lookahead that matches a location in string that is not immediately followed with a whitespace
[A-Za-z0-9_\s]* - zero or more (due to the * quantifier) ASCII letters, digits, underscores and whitespaces
$ - end of string.
You are looking for lookaheads:
^(?! )[ \w]+$
\w is a short form for [\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\u200c\u200d] (see here and here for more information) as it is used very often, but see #Wiktor's comment for a more precise clarification.
Also,see a demo on regex101.com.

How to replace unicode escape character in Dart

I need to clean up a string that has an escape character but couldn't do so.
Here is my test code:
test('Replace unicode escape character', () {
String originalText = 'Jeremiah 52:1\\u201334';
String replacedText = originalText.replaceAll(r'\\', r'\');
expect(replacedText, 'Jeremiah 52:1\u201334');
});
It fails with an error:
Expected: 'Jeremiah 52:1–34'
Actual: 'Jeremiah 52:1\\u201334'
Which: is different.
Expected: ... miah 52:1–34
Actual: ... miah 52:1\\u201334
Unicode characters and escape characters aren't stored the way you write them when you wrote the string -- they are converted to their own values. This is evident when you run the following code:
print('\\u2013'.length); // Prints: 6
print('\u2013'.length); // Prints: 1
Here, what happened was: the first stored the following characters: '\', 'u', '2', '0', '1', and '3' -- while the latter stored '–' only.
Hence, your attempt to change the first by replacing two slashes \\ with one slashes \ wouldn't work, as the compiler isn't converting your unicode escape characters any longer.
That doesn't mean that you won't be able to convert your unicode codes into unicode characters though. You could use the following code:
final String str = 'Jeremiah 52:1\\u2013340';
final Pattern unicodePattern = new RegExp(r'\\u([0-9A-Fa-f]{4})');
final String newStr = str.replaceAllMapped(unicodePattern, (Match unicodeMatch) {
final int hexCode = int.parse(unicodeMatch.group(1), radix: 16);
final unicode = String.fromCharCode(hexCode);
return unicode;
});
print('Old string: $str');
print('New string: $newStr');

SWIFT: Replace double backslash with single backslash in a string

I have a string that has the value of "\\u{abc}" and I want to change it to "\u{abc}"
My code is as it follows
str = "\\u{abc}"
let newstr = str.remove(at: str.startIndex)
print(newstr)
The output is: u{abc}
How can I remove just the first one ?
I also tried using replacing occurrences and dropfirst
Does anyone know how to fix this
Thanks for your time
i am editing the answer sorry i made huge mistake, here is the correction
var str = "\\u{abc}"
print(str.replacingOccurrences(of: "\\", with: #"\"#))
ouput : \u{abc}
Let's go
if you try to just
print("\u")
Xcode will warn you: Expected hexadecimal code in braces after unicode escape.
so, Xcode expecting some hexadecimal value, and this is where the conflict begins
your solution, specifically with \U is:
let newstr = "\\" + str.dropFirst()
Full is:
var str = "\\u{abc}"
let newstr = "\\" + str.dropFirst()
print(newstr)

How to add Unicode escape sequence in Localizable.strings?

How can a Unicode escape sequence be added to a string in Localizeable.strings file if the string is casted to NSString?
Here is one (ugly) example:
// Localized string: "\u{200F}Number %#" = "\u{200E}Number %#";
let string = NSMutableAttributedString(string: NSString(format: NSLocalizedString("Number %#", comment: "") as NSString, aNumber as NSNumber)) as String
From this question I understand that the problem is the incompatible escape sequences of Localizeable.strings and NSString.
Adding the unicode characters directly is Localizeable.strings file is not an option because I need to insert bidirectional semantics markers that are not printable characters. They would also be lost in most translation programs.
How can I work around that?

Invalid escape sequence in literal: "\b"

I need to be able to create a String that is "\b". But when I try to, Xcode throws a compile-time error: Invalid escape sequence in literal. I don't understand why though, "\r" works just fine. If I put "\\b" then that's what is actually stored in the String, which is not what I need - I only need one backslash. To me, this seems to be a Swift oddity because it works just fine in Objective-C.
let str = "\b" //Invalid escape sequence in literal
NSString *str = #"\b"; //works great
I need to generate this string because "\b" is the only way to detect when the user pressed 'delete' when using UIKeyCommand:
let command = UIKeyCommand(input: "\b", modifierFlags: nil, action: "didHitDelete:")
How can I work around this issue?
EDIT: It really doesn't want to generate a String that is only "\b", this does not work - it stays the original value:
var delKey = "\rb"
delKey = delKey.stringByReplacingOccurrencesOfString("r", withString: "", options: .LiteralSearch, range: nil)
The Swift equivalent of \b is \u{8}. It maps to ASCII control code 8, just like \b in Objective C. I've tested this and found it to work fine with UIKeyCommand, in this earlier answer of mine.
Example snippet:
func keyCommands() -> NSArray {
return [
UIKeyCommand(input: "\u{8}", modifierFlags: .allZeros, action: "backspacePressed")
]
}
I don't believe it is supported.
Based on the Swift documentation https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html:
String literals can include the following special Unicode characters:
The escaped special characters \0 (null character), \ (backslash), \t
(horizontal tab), \n (line feed), \r (carriage return), \" (double
quote) and \' (single quote)
An arbitrary Unicode scalar, written as
\u{n}, where n is between one and eight hexadecimal digits
The ASCII for the \b is 8. If you do the following, you'll see these results
let bs = "\u{8}"
var str = "Simple\u{8}string"
println(bs) // Prints ""
println("bs length is \(bs.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1
println(str) // Prints Simplestring
let space = "\u{20}"
println(space) // Prints " "
println("space length is \(space.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1
str = "Simple\u{20}string"
println(str) // Prints Simple string
It looks like while ASCII 8 "exists", it is "ignored".

Resources