473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find a word in a string

Hi

I have a string containing a phrase, and I want to search for a
particular word and get its index. I've tried a couple of things
already:

InStr(phrase, word) - this doesn't work because it finds "subwords",
eg. If I search for "colour" in a phrase containing "colourful", it'll
still detect it.

Regex.IsMatch(phrase, "\b" & word & "\b") - this doesn't work because
it only returns True or False, and doesn't actually tell me where in
the phrase the word is.

Basically I seem to be looking for a "hybrid" solution; one that will
find a whole word, and return its index. What should I be doing?

Thanks
Chris

Apr 18 '07 #1
5 11670
wouldn't you want to search for " colour "? with a space before and after?
this would miss if it was the first word plus words at the end of a sentence,
question etc

if phrase.indexof(" colour ") then
found = true
end if
Hi

I have a string containing a phrase, and I want to search for a
particular word and get its index. I've tried a couple of things
already:

InStr(phrase, word) - this doesn't work because it finds "subwords",
eg. If I search for "colour" in a phrase containing "colourful", it'll
still detect it.

Regex.IsMatch(phrase, "\b" & word & "\b") - this doesn't work because
it only returns True or False, and doesn't actually tell me where in
the phrase the word is.

Basically I seem to be looking for a "hybrid" solution; one that will
find a whole word, and return its index. What should I be doing?

Thanks
Chris

Apr 18 '07 #2
On Apr 17, 9:04 pm, Chris Mahoney <chrismaho...@gmail.comwrote:
Hi

I have a string containing a phrase, and I want to search for a
particular word and get its index. I've tried a couple of things
already:

InStr(phrase, word) - this doesn't work because it finds "subwords",
eg. If I search for "colour" in a phrase containing "colourful", it'll
still detect it.

Regex.IsMatch(phrase, "\b" & word & "\b") - this doesn't work because
it only returns True or False, and doesn't actually tell me where in
the phrase the word is.

Basically I seem to be looking for a "hybrid" solution; one that will
find a whole word, and return its index. What should I be doing?

Thanks
Chris
Why not use something like:

' Typed in message
Match m = Regex.Match(phrase, String.Format("\b{0}\b", word)).

Then you can access both the index of the string and it's length from
the match object.

Thanks,

Seth Rowe

Apr 18 '07 #3
On Apr 18, 1:41 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
Why not use something like:

' Typed in message
Match m = Regex.Match(phrase, String.Format("\b{0}\b", word)).
Thanks Seth, that's got me started. As you can probably tell, I'm new
to Regex :)

Now I'm trying to figure out how to add a "start point" to the Regex
search. With InStr, I could do InStr(10, phrase, word) and it'd start
searching from the 10th position (ignoring anything prior). Is there a
way to accomplish this with Regex too?

Jay, I'd considered using a space but the word I'm searching for may
be at the front of the paragraph or surrounded by punctuation. Using
\b in a Regex appears to allow for this.

Chris

Apr 18 '07 #4
On Apr 17, 10:33 pm, Chris Mahoney <chrismaho...@gmail.comwrote:
On Apr 18, 1:41 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
Why not use something like:
' Typed in message
Match m = Regex.Match(phrase, String.Format("\b{0}\b", word)).

Thanks Seth, that's got me started. As you can probably tell, I'm new
to Regex :)

Now I'm trying to figure out how to add a "start point" to the Regex
search. With InStr, I could do InStr(10, phrase, word) and it'd start
searching from the 10th position (ignoring anything prior). Is there a
way to accomplish this with Regex too?

Jay, I'd considered using a space but the word I'm searching for may
be at the front of the paragraph or surrounded by punctuation. Using
\b in a Regex appears to allow for this.

Chris
I don't know of a way to define a start point for regex, expect if you
pass a substring of the phrase into the function - but then you'll
need to adjust the index to find the original position of the string.
You will also take a minor performance hit for creating the substring
(this won't matter unless you do it millions of times back to back).
Besides, regex runs extremely fast, so you probably won't notice the
difference between changing the start points.

Also, you may look at downloading expresso, it's a great tool for
building/evaluating regular expressions, and it is definitely helping
me out with my current project.

http://www.ultrapico.com/Expresso.htm

Thanks,

Seth Rowe

Apr 18 '07 #5
On Apr 18, 10:35 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
I don't know of a way to define a start point for regex, expect if you
pass a substring of the phrase into the function - but then you'll
need to adjust the index to find the original position of the string.
You will also take a minor performance hit for creating the substring
(this won't matter unless you do it millions of times back to back).
Besides, regex runs extremely fast, so you probably won't notice the
difference between changing the start points.
It's not the cleanest code ever, but I've written a new InStr that
accepts a Regex and can take indices :)

Function RegexInStr(ByVal Start As Integer, ByVal String1 As String,
ByVal String2 As String) As Integer
If String1.Length < Start Then Return 0
Dim intCharsToRemove As Integer = Start - 1
Dim strToTest As String = Strings.Right(String1, String1.Length -
intCharsToRemove)
Dim intIndex As Integer =
System.Text.RegularExpressions.Regex.Match(strToTe st, String2).Index +
1
If intIndex = 1 And Strings.Left(strToTest, String2.Length) <>
String2 Then Return 0
Return intIndex + intCharsToRemove
End Function

My problem is now solved, and I thought I'd just post my code here in
case someone else runs into a similar problem.

Thanks for all your help!
Chris

Apr 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
5
by: Paula | last post by:
Hi !! I have to find some words in a string. I can use string.IndexOf, LastIndexOf, etc, but they are case sensitive. And there is another problem : If I found the word, I have to get three...
2
by: Al_P via DotNetMonster.com | last post by:
I have: Win2K Office2000 Working in VB.Net (2003) My .Net project has a reference to Word 9.0 Instantiating things in a simple and straightforward manner: Dim word as Word.Application Dim...
19
by: ashmangat | last post by:
Hi! now on the chapter "string-class" My assignment is below Word Counter: Write a function that accepts a pointer to a C-String as an argument and returns the number of words contained in the...
2
by: Gaijinco | last post by:
I was trying a function that read an string (which previously had attached two special characters & for the beginning of the word and # for the end of the word) and if it was "<word>ize" it changed...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
10
by: Johny | last post by:
I need to find all the same words in a text . What would be the best idea to do that? I used string.find but it does not work properly for the words. Let suppose I want to find a number 324 in...
1
by: xetulul | last post by:
my problem is that im unable to match a user input word to a word in a string. the strings are in a file and then placed in nodes. i have to go through each token in each node to find the 'word'....
0
by: zaza24 | last post by:
Hello there I am loosing my time since a few days trying to install a CRM (ACT! 2008 version 10) software on a brand new HP notebook with Vista prof OS. Ths installation do not show any...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.