473,804 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(p hrase, "\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 11710
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(p hrase, "\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...@g mail.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(p hrase, "\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(phr ase, 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...@yah oo.comwrote:
Why not use something like:

' Typed in message
Match m = Regex.Match(phr ase, 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...@g mail.comwrote:
On Apr 18, 1:41 pm, rowe_newsgroups <rowe_em...@yah oo.comwrote:
Why not use something like:
' Typed in message
Match m = Regex.Match(phr ase, 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...@yah oo.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(ByVa l Start As Integer, ByVal String1 As String,
ByVal String2 As String) As Integer
If String1.Length < Start Then Return 0
Dim intCharsToRemov e As Integer = Start - 1
Dim strToTest As String = Strings.Right(S tring1, String1.Length -
intCharsToRemov e)
Dim intIndex As Integer =
System.Text.Reg ularExpressions .Regex.Match(st rToTest, String2).Index +
1
If intIndex = 1 And Strings.Left(st rToTest, String2.Length) <>
String2 Then Return 0
Return intIndex + intCharsToRemov e
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
14941
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 in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
5
7842
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 words before and after the found word . Example:
2
2182
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 doc As Word.Document Dim range As Word.Range
19
9592
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 string. For instance, if the string argument is "Four score and seven years ago" the function should return the number 6. Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The number of...
2
1762
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 into "change into <word>". I did this function: bool Ize(string& s) { bool is=false; string eval="ize"; int end=s.find('#');
1
4219
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: (there is a WORD in ( my string WORD )) and * WORD * to (find WORD) and * WORD * Should give me the to word between star (star ar not part of string)
10
3924
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 the text '45 324 45324' there is only one occurrence of 324 word but string.find() finds 2 occurrences ( in 45324 too)
1
3967
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'. but my code doesn't work. how can i make my code to understand that word: "night" == token in node: "night." . import java.util.*; import java.io.*; import java.util.StringTokenizer; class edit
0
2771
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 troubles and works fine; the software installs also Microsoft SQL Server Express 2005 (the different services installed are SQL Server for ACT7, SQL server Active Directory Helper, SQL Server Browser and SQL Server VSS Writer). SQL Service for ACT7 is in...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.