Jump to content

substring search not excluding extra characters


ur
 Share

Recommended Posts

I am trying to getan exact word match in searching a substring using function StringInStr.

Like my search word is oFSO sentence may contain oFSO or OFSO= and not case-sensitive.so I created conditional syntax as

$sentence is the complete sentence and $searchword contains the searching word.

If StringInStr($sentence, " "&$searchword&" ") or StringInStr($sentence, " "&$searchword&"=")

It is working fine for most cases and it excluding words like ofs,ofs1 ,etc words

But if the word is ofso1 (i.e., contains the word we specified + extra characters) then it is not excluding that.

Could you please help how to exclude that and write condition so that it will get exact match i.e, oFSO or ofso or OFSO (not case-sensitive).

 

Edited by ur
Link to comment
Share on other sites

  • Moderators

ur,

Please do not double-post - I have deleted the other one.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Why StringInStr? What about regexp?

I didn't tried regexp. It is bit complicated and I felt SubInStr is easy to use and the $searchword is variable not fixed to particular word.

But anything is fine. Can you tell any solution for particular context.

Edited by ur
Link to comment
Share on other sites

Sorry, the above script is working fine the problem is not with the extra characters appended.

But in my script when I need to search for exact word.As a previous solution I used
StringInStr($sentence, " "&$searchword&" ")

But I need to include words even if the sentence used $searchword and appended the word with any special characters like (=,.,& etc) but exclude if any numbers or alphabets appended to it.

Ex:    sunday12 should be excluded but sunday= or sunday& should not be excluded.

Is there any regular expression we can use in SubInStr or any other function to get this solution.

Link to comment
Share on other sites

$str = "oFSO oFSo= or mofso or oFSo& ofs,ofs1 ofso1 OFSO"
$s = "ofso"

$res = StringRegExpReplace($str, '(?i)\b' & $s & '\b[[:^alnum:]]?', "#")
msgbox(0,"", $res)

If StringRegExp($str, '(?i)\b' & $s & '\b') Then Msgbox(0,"", "match")

This expression matches any substring $s followed (or not) by a non-alphanumeric char

Edited by mikell
2nd regex fixed
Link to comment
Share on other sites

mikell,

The part [[:^alnum:]]? is superfluous since if \b wouldn't be true if an alphanum was there after $s.

Also \b relies on the definition of \w and that includes the underscore, which may be something the OP doesn't want.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

In such case, a negative lookahead assertion is simpler and covers all bases, like $s & "(?![[:alnum:]]) but what to put inside the assertion depends on what the OP doesn't want.

Edited by jchd
Too much SQL or C implies using != too easily!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

My task is I am looking for a particular set of words by reading it from a file.The complete file is loaded to a variable $strScript.

Now I am searching with the expression provided above 

$res = StringRegExpReplace($str, '(?i)\b' & $s & '\b[[:^alnum:]]?', "#")

But we need to exclude the word if it is in the double quotes.

Ex: I am searching for sunday.

And if the file contains 10 lines and if the word we found at line 8 then we also need to check whether the word is in a double quotes.
"One of the holiday is sunday for this week.".

If it is double quotes then we need to exclude that.

Link to comment
Share on other sites

This should give you the idea:

$str = '"One of the holiday is sunday for this week." ... and today is not sunDay (but I love Sundays even if today is not sunday!)'
$s = "SuNDay"

ConsoleWrite($str & @LF)
$res = StringRegExpReplace($str, '".*?"', "")   ; removes anything inside pair of double quotes
ConsoleWrite($res & @LF)

$res = StringRegExpReplace($res, '(?i)\b' & $s & '(?![[:alnum:]])', '###')  ; replaces $s by ###
ConsoleWrite($res & @LF)

You can use this site to play with Perl (PCRE) regexp.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Nice site jchd, I guess there is no "find next" though?  couldn't find it.

I have been using Notepad++ it has regex option for searching but I like how this site helps tutor with the meaning of each part of your RegEx maybe this is what I needed to finally learn some of this stuff. 

Link to comment
Share on other sites

I don't get the "find next" part. I don't know which regexp flavor Motepad++ uses and which precise possibilities it offers.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

×
×
  • Create New...