Reputation: 1995
I want to get all the occurences of a specific string, e.g. police officer
in a text file, the text file has a lot of whitespace and it could potentially look like this:
T h e r a p i s t r a n f r o m t h e p o l i c e
o f f i c e r, d o w n M a i n S t r e e t.
Or in the simplest form:
The rapist ran from the police officer, down Main Street.
I am knowledgeable in VBA or PHP, but would be open to other quick solutions.
I was thinking about Regex, but wasnt sure, if there was a way.
Upvotes: 0
Views: 109
Reputation: 591
You could do something like this (in php):
1) Read contents of the file into a string (or the contents of the line if you're doing it line by line)
2) Use str_replace() to strip any white space from the string
3) Use strpos() to see if 'policeofficer' is in the string. (Note that you'll need to strip spaces from the input too).
4) return the result of strpos (Note that you'll need to use === to test).
I'm not sure this is the most efficient way to do this, but it should work.
Upvotes: 0
Reputation: 11
You can put spaces in regex, your search string here would be something like p\s*o\s*l\s*i\s*c\s*e\s+o\s*f\s*f\s*i\s*c\s*e\s*r\s*
. If you want to include tabs, you can change \s*
to [\s\t]*
. Of course, you can create these search strings automatically.
If you want to try out regular expressions first, there are numerous online resources for this, for example http://regexpal.com/
Upvotes: 1