Get value from string if finded match regex - ios

Imagine i have a list of string´s like this:
Hello Word 132 132 132 GoodBye!! Should return 132132132
Hello Word 132 132 GoodBye!! Should return nil
132 132 132 GoodBye! Should return 132132132
132132132 GoodBye! Should return 132132132
1321321321GoodBye! Should return nil
132 132 1321 Should return nil
How can i check whether the phrase has 9 followed algorithms, or separated by space, and get that same number?
Thanks

You can use this regex
(\d{3})(\s?\1){2}
and remove any whitespace in the match.
DEMO BTW, if you don't want to match in
Some123 123 123Thing
you can use word boundaries \b(\d{3})(\s?\1){2}\b

Related

'NSConcreteMutableAttributedString initWithString:: nil value' when using Google Nearby Messages

I was following Google's documentation for using NearbyMessages API. I'm using Swift3. I have the following code to set up the app to publish to nearby.
let messageManager = GNSMessageManager(apiKey: "someky")
var publication: GNSPublication?
let message = GNSMessage(content:"name".data(using: .utf8))
if let messageManager = messageManager, let message = message {
publication = messageManager.publication(with:message)
}
I ran the app on the simulator. Then the app crashes in the 5th line (The line in the if-let block). Here's part of the stack trace:
(
0 CoreFoundation 0x000000011302334b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000011271e21e objc_exception_throw + 48
2 CoreFoundation 0x000000011308c265 +[NSException raise:format:] + 197
3 Foundation 0x0000000112229fdc -[NSConcreteMutableAttributedString initWithString:] + 129
4 ProjectLibs 0x000000011879b55a gns_localizedAllowAppToConnectAttributedString + 415
5 ProjectLibs 0x000000011879b8b9 gns_localizedAllowAppToConnectString + 75
6 ProjectLibs 0x000000011879ae9f gcp_verifyDealloc + 24324
)
Since the messageManager and the message are both protected by the if-let block, I cannot find where is the nil exception coming from. Does anyone know why would there be a nil value in this block of code?
Thank you very much!
Does it work on a device? There is a bug in Nearby Messages 1.1.0 that causes a nil pointer exception on the simulator when you use BLE as a discovery medium.
Apologies! We'll get the bug fixed in the next release.

Insert sections at the beginning of UICollectionView

I want to add some sections to UICollectionView. insertSections at the index zero didn't work for me. So my idea was to insertSections at the end and then use moveSection:toSection: to move the elements from the end to the beginning. Here I get
NSInternalInconsistencyException Reason: attempt to move section 36, but there are only 36 sections before the update
I can only provide C# code, but you can also post Objective-C/Swift.
this.controller.CollectionView.PerformBatchUpdatesAsync (
delegate() {
nint sectionsBefore = this.controller.CurrentNumberOfSections;
this.controller.CurrentNumberOfSections += 12;
this.controller.CollectionView.InsertSections(NSIndexSet.FromNSRange(new NSRange(sectionsBefore,12)));
for(nint i=sectionsBefore; i<=this.controller.CurrentNumberOfSections; i++){
this.controller.CollectionView.MoveSection(i,0);
}
}
);
Edit:
Here is an output of the variables:
sectionsBefore: 36
CurrentNumberOfSections: 48
Range: <NSIndexSet: 0x7a77b9b0>[number of indexes: 12 (in 1 ranges), indexes: (36-47)]
36
37
38
39
40
41
42
43
44
45
46
47
If you have 36 sections then then your valid section indexes are 0-35, but your loop will end up calling MoveSection(36, 0). You should use < instead of <=.

How to show hash (#) before extension in libphonenumber?

I would like to generate number with a hash (#) to produce number that can be dialed with extension.
How to do this?
I have following code:
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Iterable<PhoneNumberMatch> matches = phoneNumberUtil.findNumbers("0730506760 int 120", "RO");
for(PhoneNumberMatch match: matches){
PhoneNumber number = match.number();
System.out.println(phoneNumberUtil.format(number, PhoneNumberFormat.E164) );
System.out.println(phoneNumberUtil.format(number, PhoneNumberFormat.INTERNATIONAL) );
System.out.println(phoneNumberUtil.format(number, PhoneNumberFormat.NATIONAL) );
System.out.println(phoneNumberUtil.format(number, PhoneNumberFormat.RFC3966) );
System.out.println(phoneNumberUtil.formatOutOfCountryCallingNumber(number, "GB") );
System.out.println(phoneNumberUtil.formatOutOfCountryCallingNumber(number, "US"));
}
Which produces:
+40730506760
+40 730 506 760 int 120
0730 506 760 int 120
tel:+40-730-506-760;ext=120
00 40 730 506 760 int 120
011 40 730 506 760 int 120
What I want:
+40730506760#120
Does anyone knows how should I format this number to achieve desired result?
The simplest way I see would be a workaround. Use System.out.println(phoneNumberUtil.format(number, PhoneNumberFormat.INTERNATIONAL) ); and replace spaces by "" and "int" by "#".

why is code not executing on return from Future in Dart program

Could someone please explain to me why in the following code (using r25630 Windows), the value of iInsertTot at line 241 is null, or more to the point, why is line 234 ("return iInsertTot;") not executed and therefore at line 241, iInsertTot is null. The value of iInsertTot at lines 231/232 is an integer. While I can and probably should code this differently, I thought that I would try and see if it worked, because my understanding of Futures and Chaining was that it would work. I have used “return” in a similar way before and it worked, but I was returning null in those cases (eg. line 201 below).
/// The problem lines are :
233 fUpdateTotalsTable().then((_) {
234 return iInsertTot;
235 });
While running in the debugger, it appears that line 234 “return iInsertTot;” is never actually executed. Running from command line has the same result.
The method being called on line 233 (fUpdateTotalsTable) is something I am just in the process of adding, and it consists basically of sync code at this stage. However, the debugger appears to go through it correctly.
I have included the method “fUpdateTotalsTable()” (line 1076) just in case that is causing a problem.
Lines 236 to 245 have just been added, however just in case that code is invalid I have commented those lines out and run with the same problem occurring.
218 /*
219 * Process Inserts
220 */
221 }).then((_) {
222 sCheckpoint = "fProcessMainInserts";
223 ogPrintLine.fPrintForce ("Processing database ......");
224 int iMaxInserts = int.parse(lsInput[I_MAX_INSERTS]);
225 print ("");
226 return fProcessMainInserts(iMaxInserts, oStopwatch);
227 /*
228 * Update the 'totals' table with the value of Inserts
229 */
230 }).then((int iReturnVal) {
231 int iInsertTot = iReturnVal;
232 sCheckpoint = "fUpdateTotalsTable (insert value)";
233 fUpdateTotalsTable().then((_) {
234 return iInsertTot;
235 });
236 /*
237 * Display totals for inserts
238 */
239 }).then((int iInsertTot) {
240 ogTotals.fPrintTotals(
241 "${iInsertTot} rows inserted - Inserts completed",
242 iInsertTot, oStopwatch.elapsedMilliseconds);
243
244 return null;
245 /*
192 /*
193 * Clear main table if selected
194 */
195 }).then((tReturnVal) {
196 if (tReturnVal)
197 ogPrintLine.fPrintForce("Random Keys Cleared");
198 sCheckpoint = "Clear Table ${S_TABLE_NAME}";
199 bool tClearTable = (lsInput[I_CLEAR_YN] == "y");
200 if (!tFirstInstance)
201 return null;
202 return fClearTable(tClearTable, S_TABLE_NAME);
203
204 /*
205 * Update control row to increment count of instances started
206 */
207 }).then((_) {
1073 /*
1074 * Update totals table with values from inserts and updates
1075 */
1076 async.Future<bool> fUpdateTotalsTable() {
1077 async.Completer<bool> oCompleter = new async.Completer<bool>();
1078
1079 String sCcyValue = ogCcy.fCcyIntToString(ogTotals.iTotAmt);
1080
1081 print ("\n********* Total = ${sCcyValue} \n");
1082
1083 oCompleter.complete(true);
1084 return oCompleter.future;
1085 }
Your function L230-235 does not return anything and that's why your iInsertTot is null L239. To make it work you have to add a return at line 233.
231 int iInsertTot = iReturnVal;
232 sCheckpoint = "fUpdateTotalsTable (insert value)";
233 return fUpdateTotalsTable().then((_) {
234 return iInsertTot;
235 });

Sort rps-blast results by position of the hit

I'm beginning with biopython and I have a question about parsing results. I used a tutorial to get involved in this and here is the code that I used:
from Bio.Blast import NCBIXML
for record in NCBIXML.parse(open("/Users/jcastrof/blast/pruebarpsb.xml")):
if record.alignments:
print "Query: %s..." % record.query[:60]
for align in record.alignments:
for hsp in align.hsps:
print " %s HSP,e=%f, from position %i to %i" \
% (align.hit_id, hsp.expect, hsp.query_start, hsp.query_end)
Part of the result obtained is:
gnl|CDD|225858 HSP,e=0.000000, from position 32 to 1118
gnl|CDD|225858 HSP,e=0.000000, from position 1775 to 2671
gnl|CDD|214836 HSP,e=0.000000, from position 37 to 458
gnl|CDD|214836 HSP,e=0.000000, from position 1775 to 2192
gnl|CDD|214838 HSP,e=0.000000, from position 567 to 850
And what I want to do is to sort that result by position of the hit (Hsp_hit-from), like this:
gnl|CDD|225858 HSP,e=0.000000, from position 32 to 1118
gnl|CDD|214836 HSP,e=0.000000, from position 37 to 458
gnl|CDD|214838 HSP,e=0.000000, from position 567 to 850
gnl|CDD|225858 HSP,e=0.000000, from position 1775 to 2671
gnl|CDD|214836 HSP,e=0.000000, from position 1775 to 2192
My input file for rps-blast is a *.xml file.
Any suggestion to proceed?
Thanks!
The HSPs list is just a Python list, and can be sorted as usual. Try:
align.hsps.sort(key = lambda hsp: hsp.query_start)
However, you are dealing with a nested list (each match has a list of HSPs), and you want to sort over all of them. Here making your own list might be best - something like this:
for record in ...:
print "Query: %s..." % record.query[:60]
hits = sorted((hsp.query_start, hsp.query_end, hsp.expect, align.hit_id) \
for hsp in align.hsps for align in record.alignments)
for q_start, q_end, expect, hit_id in hits:
print " %s HSP,e=%f, from position %i to %i" \
% (hit_id, expect, q_start, q_end)
Peter

Resources