Help with Regex Find

G

Guest

Hi,
How can I implement regex to find complete or partial words or a group of
words. Similar to the "Find" in MS Word. I need to scan text files eg

look for "test"
or "this is a test"
or
"this is a te"

Thanks
 
O

Oliver Sturm

Hello Chris,

I'm not sure I understand your question. See below.
How can I implement regex to find complete or partial words or a group of
words. Similar to the "Find" in MS Word. I need to scan text files eg

look for "test"

Okay, so in your example we're searching for the word "test", right? Why
would I need a regular expression to search for a literal string?
or "this is a test"

I understand that here the string would be found. Right?
or
"this is a te"

Here the string would obviously not be found. Right?

My suspicion is that somehow you want to say "no" to my last "right?"
question. In that case - how do you imagine your search algorithm to work?
Forget the regular expressions for a second, what do you expect to happen?
Are we talking about an incremental search here (you type, and matches are
found while you do so)?

Please explain some more of what you want to do.


Oliver Sturm
 
G

Guest

Yes sort of incremental. So if I choose to find "test" it should find "test".
If I choose to find "this is a test" it should find. Same goes for "this is
a".

Basically whet string I ask to find in the text file, it should search for
exactly that.

Thanks
 
O

Oliver Sturm

Hello Chris,

Hm, I don't know. I seem to be more dense than I thought :)
Yes sort of incremental. So if I choose to find "test" it should find
"test".

Okay, that sounds logical.
If I choose to find "this is a test" it should find.

Hm, I don't understand that part. There seems to be something missing at
the end of your sentence.
Same goes for "this is a".

Same as above - I don't know what this relates to.
Basically whet string I ask to find in the text file, it should search for
exactly that.

Okay, this is a clear enough statement. But I don't understand how you
imagine regular expressions to come into play here - it sounds like you
are simply searching for a literal string each time round. Otherwise I'm
sorry to say I'm still not getting it.


Oliver Sturm
 
G

Guest

Hi,
Sorry about that. I basically want to search for whatever string is
passed/given? Exactly like the "Find" in office or ie.
 
O

Oliver Sturm

Hello Chris,
Sorry about that. I basically want to search for whatever string is
passed/given? Exactly like the "Find" in office or ie.

Well, that certainly breaks it down nice and easy :) But then, what's
your problem with it?

I'm assuming you must be storing the text content you want to search in a
string variable, or more probably in a collection of string variables. So
an algorithm to search for a particular literal could look as simple as
this:


foreach(string str in allMyStrings) {
int index = str.IndexOf(mySearchString);
if (index != -1) {
// found it, now do something with it!
}
}


Oliver Sturm
 
O

Oliver Sturm

Hello Chris,
Ah ha.......I'll give it a shot. Thanks! Can this handle large text files?

Well, this is hardly a word processing application at this point :)
Generally, yes... it can handle as much data as you can store in memory.
If searching is a major feature of your application, you may want to look
into some indexing technology to make it more efficient, but as you
mentioned functionality "like Word", I'm assuming that is not so relevant
to you.

Otherwise, you'll hit a bottle neck when your file is too large to be
loaded into memory - if that is the case, a "streaming" approach will be
more useful, where you load data from the hard drive line by line (or
buffer by buffer, in any case), search it and forget it, thereby never
keeping the whole file in memory at once.


Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top