Sign in to follow this  
Followers 0
AutoCraig

Find Whole Words Only

5 posts in this topic

Is there a way to have a general "find whole words only" function? Here is was I'm trying to do:

I have this line of text, A 1130 825 20 0 1 3 REFDES=R27, and if I do a StringInStr compare with REFDES=R2, it will find it. I want it to find only REFDES=R27. I've been trying to fool around with StringRegExp but have had no luck. Any hints would be much appreciated, thanks!

Share this post


Link to post
Share on other sites



I'm not seeing what your problem is, you're asking the function to look for a string, and it's finding the string you wanted. If you want it to only find "REFDES=R27" then shouldn't you be using that to search for instead a partial matching string?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Yup, a bit more explanation is needed, meanwhile play around with this :)

$sLine = "A 1130 825 20 0 1 3 REFDES=R27"
$avMatches = StringRegExp($sLine, "^\w{1}\ \d{4}\ \d{3}\ \d{2}\ \d{1}\ \d{1}\ \d{1}\ (\w{6})=(\w\d{2})$", 1)
MsgBox(64, "Demo 1", $avMatches[0] & "=" & $avMatches[1])

Share this post


Link to post
Share on other sites

I'm not seeing what your problem is, you're asking the function to look for a string, and it's finding the string you wanted. If you want it to only find "REFDES=R27" then shouldn't you be using that to search for instead a partial matching string?

You're right I need more info. I'm storing a text file line by line in an array. So a particular line in that text file contains A 1130 825 20 0 1 3 REFDES=R27. I have another text file that tells me what REFDES=R27 needs to change to. The text file contains what the REFDES was and what the REFDES should be. Looks something like this:

Was Is

R27 R10

R100 R70

R2 R80

So I want to compare the A 1130 825 20 0 1 3 REFDES=R27 string to the 'was is' text file and replace the string with the new value (R10 in this case). The issue is that the script needs to run through the whole 'was is' text file to find the matches so if it came across R2 first it would mistakenly replace R27 with R807 because it would see the R2 inside of the R27. I probably made that way more complicated then it should be and there is probably an easy answer that I'm missing. In the end I need the StringInStr and/or the StringReplace function to find whole words only so when it looks for R27 it only looks for R27 and not R2.

Share this post


Link to post
Share on other sites

Ok, :) now that we have a slight idea of what you want, use _FileReadToArray(), StringRegExp() StringRegExpReplace(); Post the full code you have tried so far so we can give you more relevant help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0