I am using RegexKitLite in an iPhone project and want to use regex to find words that start with the #-sign. For instance, "#home #chores", when searched, would return both words.
The regex string I am using is "(?m-s:#.*\\s*)". When I use this, though, I get a crash. When I use the same thing, but with a # instead of #, it works just fine: "(?m-s:#.*\\s*)". WTF?
I would much appreciate it if someone with a better understanding of regular expressions could help me on this. The tutorials I have seen so far have been near incomprehensible to me.
I did a modification of Manu's idea, just switching the location of the # in the regex.
/(#\b\w+)/
I tested it on a string with '#foo #bar #baz #lol' and it seemed to do what you're looking for in matching on the words and capturing them with the parens.
Have you tried something like that:
NSString *search = #"This is my #home string with #some tokens to be #found";
NSString *regex = #"\\b#(\\w+)";
NSArray *matches = NULL;
matches = [search componentsMatchedByRegex:regex];
// now matches should have { #"home", #"some", #"found" } values
I haven't tested that but should work.
This may sound too simple, but have you tried changing # to \# or \\#
Why not simply use /\b#\w+/?
Related
i am having trouble about json part of venues, in this picture i am trying to take the prefix and suffix, i am putting size between them but my problem is when i try to put them together the link of prefix + size + suffix comes like this -> i am taking prefix and suffix in seperate NSMutableArray's but when i try to join them together it's not working. and here is my way to join them.
where am i doing this wrong?
Are you sure that your objects in the imagePrefix and imageSuffix arrays are actually strings? Because judging from your logs it looks as if you're trying to concatenate two arrays and a string. If you let us know what is actually in those arrays you might get more helpful answers. You must be doing some conversion/manipulation from the original JSON, as in the API they get returned as dictionary items not arrays.
On a unrelated note, consider using fast enumeration (for id item in array) rather than writing out the for statement as you've done. Generally speaking it's also much better to post your code as text using markdown syntax rather than images: makes it much harder to copy/paste your code into an answer.
so thanks to "Matthias Bauch" i figured it out and here is my answer for my own question :)
for (int e = 0; e<=[imagePrefix count]-1; e++) {
NSLog(#"%#b_32%#", [[imagePrefix objectAtIndex:e] objectAtIndex:0], [[imageSuffix objectAtIndex:e] objectAtIndex:0]);
}
Thanks guys!
i am using whoosh to index over 200,000 books. but i have encountered some problems with it.
the whoosh query parser returns NullQuery for words like "C#", "C++" with meta-characters in them and also for some other short words. this words are used in the title and body of some documents so i am not using keyword type for them. i guess the problem is in the analysis or query-parsing phase of searching or indexing but i can't touch my data blindly. can anyone help me to correct this issue. Tnx.
i fixed the problem by creating a StandardAnalyzer with a regex pattern that meets my requirements,here is the regex pattern:
'\w+[#+.\w]*'
this will make tokenizing of fields to be done successfully, and also the searching goes well.
but when i use queries like "some query++*" or "some##*" the parsed query will be a single Every query, just the '*'. also i found that this is not related to my analyzer and this is the Whoosh's default behavior. so here is my new question: is this behavior correct or it is a bug??
note: removing the WildcardPlugin from the query-parser solves this problem but i also need the WildcardPlugin.
now i am using the following code:
from whoosh.util import rcompile
#for matching words like: '.NET', 'C++' and 'C#'
word_pattern = rcompile('(\.|[\w]+)(\.?\w+|#|\+\+)*')
#i don't need words shorter that two characters so i don't change the minsize default
analyzer = analysis.StandardAnalyzer(expression=word_pattern)
... now in my schema:
...
title = fields.TEXT(analyzer=analyzer),
...
this will solve my first problem, yes. but the main problem is in searching. i don't want to let users to search using the Every query or *. but when i parse queries like C++* i end up an Every(*) query. i know that there is some problem but i can't figure out what it is.
I had the same issue and found out that StandardAnalyzer() uses minsize=2 by default. So in your schema, you have to tell it otherwise.
schema = whoosh.fields.Schema(
name = whoosh.fields.TEXT(stored=True, analyzer=whoosh.analysis.StandardAnalyzer(minsize=1)),
# ...
)
I have a Json return that has a string that sometimes inludes something like \Uf604 in the array (IE memo = "\Uf604";). I need to convert it to \U0001F604 if possible.
I tried to do something like stringByReplacingOccurrencesOfString but at that point when its in a string and it's been converted to ÔòÑ which I think it needs to be üòÑ to be displayed as a emoticon. I also tried
[str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"];
But that didn't change anything. It still gets returned as ÔòÑ.
Any help would be appreciated!
I believe str = [str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"]; is what you are trying to do.
stringByReplacingOccurrencesOfString:withString: doesn't change the string you are calling on but returns a new string with replaced characters.
How do I add a single character such as a '$' or a '+' at the beginning of an existing string?
I tried using the appendingString method but that adds the $ or + at the end of the string.
I know I can always save the $ or + in a new string and then append the other string, but I just want to know if there is a better way to do it.
Thank you.
This is actually very simple:
[#"+" stringByAppendingString:existingString];
This should definitely work for you :) And the + will be at the beginning.
Strings aren't mutable. You're creating a new string when you use stringByAppendingString: In order to prepend, you would have to make a mutable version of your existing string and then use insertString:atIndex: like so:
[[NSMutableString stringWithString:myString] insertString:#"$" atIndex:0];
Why there isn't a stringByPrependingString:, I don't know.
The best solution is the one you've already mentioned:
[#"$" stringByAppendingString:myString];
As long as the string is mutable, i.e. it is an NSMutableString you can use.
[str insertString:#"$" atIndex:0];
Have a read of the docs here, https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html
i am getting the problem when i get the result from the xml api web service i use
NSString *productName = [[paramName stringByReplacingOccurrencesOfString:#"\"" withString:#""] mutableCopy];
paramName is the argument of the function, but it can't replace the \ string which are on database
please help me out... thanks.
i only want to replace \ to none #"" but its not working now... please help me.. !!
i tried before its working but its not working now .. !! :(
Probably, you meant #"\\", not #"\"". It looks like you're trying to kill quotes, not backslashes.