Jump to content

Find in string not working for me?


BatMan22
 Share

Recommended Posts

Hey guys so I have a file with a bunch of data in it that looks like what you see below. I was hoping to delete the lines that don't have the extra information about the ID's I'm working with.. I figured that if I searched backwards, then when there is a match, I could delete the $i line in the array.. But for some reason my search isn't working.

 

Code:

Func CheckForDupsOnlyLookingAtSampleID()
    $sFileName = "\left.txt"
    Local $dumpinthis
    _FileReadToArray($sFileName, $dumpinthis, $FRTA_NOCOUNT)
    _ArrayDisplay($dumpinthis)
    For $i = 0 To UBound($dumpinthis) - 1
        Local $iIndex = _ArraySearch($dumpinthis, $dumpinthis[$i], $i + 1, 0, 0, 0, 1)
        MsgBox(0, 0, $iIndex)
;~      _ArrayDelete($dumpinthis, $i)
    Next

EndFunc   ;==>CheckForDupsOnlyLookingAtSampleID

 

Data in File I'm asking for it to search: 

MB-149289 300.1_W
LCSD-149289 300.1_W
LCS-149289 300.1_W
1711B26-001A 300.1_W <NO3; NO3_1; >
1711A89-006H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-006H 300.1_W
1711A89-005H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-005H 300.1_W
1711A89-004H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-004H 300.1_W
1711A89-003H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-003H 300.1_W
1711A89-002H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-002H 300.1_W
1711A89-001H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-001H 300.1_W
 

What I'm hoping to have after:

MB-149289 300.1_W
LCSD-149289 300.1_W
LCS-149289 300.1_W
1711B26-001A 300.1_W <NO3; NO3_1; >
1711A89-006H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-005H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-004H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-003H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-002H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >
1711A89-001H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >

 

 

Edited by BatMan22
Link to comment
Share on other sites

That's faster and works, provided the structure of the IDs complies with the pattern I guessed (else adapt the pattern) :

CheckForDupsOnlyLookingAtSampleID()

Func CheckForDupsOnlyLookingAtSampleID()
    $sFileName = "left.txt"
    Local $sText = FileRead($sFileName)
    $sText = StringRegExpReplace($sText, "(?m)^(\w{7}-\w{4} \w{3}\.\w{3})(\s<.*\R)(\g1\R?)", "$1$2")
    ConsoleWrite($sText & @LF)
EndFunc   ;==>CheckForDupsOnlyLookingAtSampleID

 

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

this is quite an old post, but the theme is recurring. See the 2nd example with the Scripting.dictionary object.

https://www.autoitscript.com/forum/topic/116156-best-way-to-compare-strings-for-duplicates/?do=findComment&comment=810752

also, how about this? Read that file into a SQLite memory db, and use SQL to manipulate?  "Select * from mytable where body like '%~%'" may still be faster...

Skysnake

Skysnake

Why is the snake in the sky?

Link to comment
Share on other sites

If ends after the first part, and starts with numbers (and not on the first line i suppose since i start with an /r/n)?  are those rules legit?

$str = "MB-149289 300.1_W" & @CRLF & _
"LCSD-149289 300.1_W" & @CRLF & _
"LCS-149289 300.1_W" & @CRLF & _
"1711B26-001A 300.1_W <NO3; NO3_1; >" & @CRLF & _
"1711A89-006H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >"& @CRLF  & _
"1711A89-006H 300.1_W" & @CRLF & _
"1711A89-005H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >"& @CRLF  & _
"1711A89-005H 300.1_W" & @CRLF & _
"1711A89-004H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >" & @CRLF  & _
"1711A89-004H 300.1_W"

msgbox(0, '' , stringregexpreplace($str , "\r\n(\d.*_.)\Z|\r\n(\d.*_.)\r\n" , @CRLF))

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

$str = "MB-149289 300.1_W" & @CRLF & _
"LCSD-149289 300.1_W" & @CRLF & _
"LCS-149289 300.1_W" & @CRLF & _
"1711B26-001A 300.1_W <NO3; NO3_1; >" & @CRLF & _
"1711A89-006H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >"& @CRLF  & _
"1711A89-006H 300.1_W" & @CRLF & _
"1711A89-006H 300.1_W" & @CRLF & _
"1711A89-006H 300.1_W" & @CRLF & _
"1711A89-005H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >"& @CRLF  & _
"1711A89-005H 300.1_W" & @CRLF & _
"1711A89-004H 300.1_W <Br; Cl; F; NO3; NO3_1; NO2; NO2_1; PO4_1; PO4; SO4; >" & @CRLF  & _
"1711A89-004H 300.1_W"

msgbox(0, '' , stringregexpreplace($str , '(?m)^([\w-]+\h[\w.]+)\N+\R?\K(\1\R?)*' , ""))

:)

Edited by mikell
Link to comment
Share on other sites

On 11/27/2017 at 11:51 PM, jchd said:

That's faster and works, provided the structure of the IDs complies with the pattern I guessed (else adapt the pattern) :

CheckForDupsOnlyLookingAtSampleID()

Func CheckForDupsOnlyLookingAtSampleID()
    $sFileName = "\left.txt"
    Local $sText = FileRead($sFileName)
    $sText = StringRegExpReplace($sText, "(?m)^(\w{7}-\w{4} \w{3}\.\w{3})(\s<.*\R)(\g1\R?)", "$1$2")
    ConsoleWrite($sText & @LF)
EndFunc   ;==>CheckForDupsOnlyLookingAtSampleID

 

Works perfectly. Thanks man.

Edited by BatMan22
Link to comment
Share on other sites

4 minutes ago, BatMan22 said:

Is there a better tutorial for the string to search for?

A regex workbench https://regex101.com/

AutoIt own StringRegex() help.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...