Swift Regular Expression error - regex

I create the swift regular expression to detect font.I got the following error "Type of expression is ambiguous without more context"
What wrong in my code???
let regexZG = NSRegularExpression(pattern: "\\s\u{1031}| ေ[က-အ]်|[က-အ]း", options:nil, error:nil)

By 7.3/2.2 all error parameters taking NSErrorPointer had been transitioned to throwable errors. In addition, empty option sets now use the set notation, so try:
do {
let regexZG = try NSRegularExpression(pattern: "\\s\u{1031}| ေ[က-အ]်|[က-အ]း", options:[])
// do some stuff with regexZG here
}
catch error {
print("NSRegularExpression init failed: \(error)")
// do something imaginative here
}

Related

How do I get the returning value of a function that can throw that I'm testing with XCTAssertNoThrow(...)

I want to get the return value of a function that I'm testing for a subsequent test. The function if defined like this:
func apple(banana: Banana) throws -> Cherry { ... }
I can test that it throws when it should:
XCTAssertThrowsError(try apple(banana: badBanana), "Didn't throw")
I can test it doesn't throw when it shouldn't:
XCTAssertNoThrow(try apple(banana: goodBanana), "Did throw")
I was hoping to do this:
XCTAssertNoThrow(let cherry = try apple(banana: goodBanana), "Did throw")
and then check cherry is what I would expect, but I get the following error: Consecutive statements on a line must be separated by ';'...
How can I get the returned value (an object of type Cherry in this case) from the XCTAssertNoThrow test? Or is there a better approach that I'm missing?
Many thanks
Simply call the function, and assert against the return value:
func test_apple_withGoodBanana_shouldReturnBingCherry() throws {
let result = try apple(banana: goodBanana)
XCTAssertEqual(result, .bing)
}
By marking the test method itself as throws we can call the try without ? or !. If it throws, the test will fail.
I'm using this static function:
func XCTAssertSuccessReturn<T>(
_ expression: #autoclosure () throws -> T,
in file: StaticString = #file,
line: UInt = #line
) -> T {
do {
return try expression()
} catch {
XCTFail(
error.localizedDescription,
file: file,
line: line
)
fatalError(error.localizedDescription)
}
}
Example:
let encryptedData = XCTAssertSuccessReturn(try encrypter.encrypt(data))
Here's one I like better; it doesn't cause testing to halt just because an unexpected error gets thrown. And it lets you add a message:
func CheckNoThrow<T>(
_ expression: #autoclosure () throws -> T,
_ message: #autoclosure () -> String = "",
file: StaticString = (#filePath),
line: UInt = #line
) -> T? {
var r: T?
XCTAssertNoThrow(
try { r = try expression() }(), message(), file: file, line: line)
return r
}
Every solution on here is nice with its own pros and cons. After trying them all out I like combining the error reporting of Dave and Vitali's answers with the simplicity of Jon's.
This gives information on the error thrown without a fatal error, returning an optional or executing subsequent XCTAssert statements after an error has been thrown.
func testApple() throws {
let result = XCTNoThrow(try apple(banana: goodBanana)
let result = try apple(banana: goodBanana)
XCTAssertEqual(result, .bing)
}

Script compilation failed at line 19 in procedure 'BusComp_PreWriteRecord':

I am getting the following error, when tries to save the following script.
"Script compilation failed at line 19 in procedure 'BusComp_PreWriteRecord':
Syntax error at Line 31 position 59:Expected ')'
(SBL-SCR-00128)"
function BusComp_PreWriteRecord ()
{
var obj = TheApplication().GetBusObject("Service Request");
var optybc = obj.GetBusComp("Service Request");
optybc.ActivateField("SR Type");
//optybc.ActivateField(“Typeâ€);
optybc.SetViewMode(3);
optybc.ClearToQuery();
optybc.SetSearchSpec("SR Type",this.GetFieldV alue("SR Type"));
//optybc.SetSearchSpec(“Typeâ€,this.GetFieldV alue(“Typeâ€));
optybc.ExecuteQuery(ForwardOnly);
if(optybc.FirstRecord())
{
TheApplication().RaiseErrorText("Duplicate Records");
}
}
Does anyone knows the reason for the above error?
It might be that you are missing brackets in code that is "above" PreWriteRecord()
for example these methods
Siebel compiles all event code as one large code, so missing parentheses in other places will create issue in subsequent events.

Got error of advancedBy on Swift3 conversion

I'm converting my Swift2.3 code to Swift3, and got an error at the line where I used advancedBy. XCode showing me a replacing code that using offsetBy, but I still don't understand.
func unhideEmaimage(_ imageFile: String) {
let dotLocation = imageFile.characters.index(of: ".")
self.emaImage.texture = SKTexture(imageNamed: imageFile)
if dotLocation != nil {
let filenameInitial = imageFile.startIndex
let filenameLast = dotLocation!.advancedBy(-1)
let filenamePart:String = imageFile[filenameInitial...filenameLast]
}
And, following is the code that XCode showing me how to fix the error. However, I still don't know how to modify.
let filenameLast = "String.CharacterView corresponding to your index".index(dotLocation!, offsetBy: -1)
Please let me know the way to fix this error. Thank you in advance.
You should Write this:
let filenameLast = imageFile.index(dotLocation!,offsetBy: -1)

Cannot use `replace_all` from the regex crate: expected (), found String

I'm trying to find and replace all instances of a string with a shortened version, and I want to maintain references to a capture if it's found.
I've written this code:
extern crate regex;
use regex::{Regex, Captures};
//... get buffer from stdin
let re = Regex::new(r"(capture something1) and (capture 2)").unwrap();
let out = re.replace_all(&buffer, |caps: &Captures| {
if let ref = caps.at(2).unwrap().to_owned() {
refs.push(ref.to_owned());
}
caps.at(1).unwrap().to_owned();
});
Unfortunately compilation fails with the error:
src/bin/remove_links.rs:16:18: 16:29 error: type mismatch resolving `for<'r, 'r> <[closure#src/bin/remove_links.rs:16:39: 22:6] as std::ops::FnOnce<(&'r regex::Captures<'r>,)>>::Output == std::string::String`:
expected (),
found struct `std::string::String` [E0271]
src/bin/remove_links.rs:16 let out = re.replace_all(&buffer, |caps: &Captures| {
^~~~~~~~~~~
src/bin/remove_links.rs:16:18: 16:29 help: run `rustc --explain E0271` to see a detailed explanation
src/bin/remove_links.rs:16:18: 16:29 note: required because of the requirements on the impl of `regex::Replacer` for `[closure#src/bin/remove_links.rs:16:39: 22:6]`
I can't make sense of it. I've also tried adding use regex::{Regex, Captures, Replacer} but that doesn't change the error at all.
As #BurntSushi5 pointed out, your closure should return a String. Here is a complete example for future reference:
extern crate regex;
use regex::{Regex, Captures};
fn main() {
let buffer = "abcdef";
let re = Regex::new(r"(\w)bc(\w)").unwrap();
let out = re.replace_all(&buffer, |caps: &Captures| {
caps.at(1).unwrap().to_owned()
});
println!("{:?}", out); // => "aef"
}

How to check whether given email address is invalid in action script 3?

I need to check whether given email address is invalid in action script. Following is the code/regex i came up with.
private function isEmailInvalid(email:String):Boolean
{
var pattern:RegExp = /(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}+/;
var result:Object = pattern.exec(email);
if(result == null) {
return true;
}
return false;
}
But it seems like above code do not cover all the test cases in the following link:
http://blogs.msdn.com/b/testing123/archive/2009/02/05/email-address-test-cases.aspx
Does anyone have better way of doing this?
Folowing are the tested valid emails i used (above function should return "false" for these):
firstname.lastname#domain.com
firstname+lastname#domain.com
email#domain.co.jp
Folowing are the invalid ones (so function should return "true" for these):
email#domain#domain.com
.email#domain.com
email..email#domain.com
plainaddress, email#domain..com
Remove the + at the last and you must need to put anchors.
^(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}$
Simplified one,
^[\w_.-]+#([\w-]+\.)+\w{2,4}$
DEMO
Try this RegExp :
RegExp = /\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/;