Jump to content

How to use StringRegExReplace to find a literal ? and replace both it and anything afterwords with nothing?


Go to solution Solved by MagnumXL,

Recommended Posts

I've looked at:

 

Below is some of the things I've tried for the ?:

\Q?\E

\?

For the anything I've tried:

.*

[\s\S]*

 

And I've tried all sorts of combinations with various parantheses, brackets, etc. None of it works for me. If I just do the anything part, it does remove stuff so I'm presuming my problem is with the ? part.

 

I'm trying to remove those characters so that the $test string will match the $ExpDir string. 

Opt("WinTitleMatchMode",2)

$hWndPP = WinGetHandle("POSPac MMS")

$ExpDir = "D:\Applanix test\AutomationTest\Exports\W0205_ERSKINE_NJ\PostProcessed 1 of 1_W0205_ERSKINE_NJ"
wLog($ExpDir)
$test = ControlGetText($hWndPP,"","[NAME:TargetFileBox]") ; D:\Applanix test\AutomationTest\Exports\W0205_ERSKINE_NJ\PostProcessed 1 of 1_W0205_ERSKINE_NJ?ct
wLog($test)
;~ $test = StringRegExpReplace($test,"\Q?\E[\s\S]*","")

$test = StringRegExpReplace($test,"\?.*","")
wLog(($test))

wLog merely writes to console with timestamp and newline

Link to comment
Share on other sites

  • Solution

"\?" will match your single question mark.

To remove the it and the rest I would use something like

"\?[^\n]+"

which technically will remove only the rest of that line but not the return character at the end.

Or

"\?.*"

which remove the ? and all characters after it, including new lines. 

Link to comment
Share on other sites

7 hours ago, MagnumXL said:

"\?.*"

which remove the ? and all characters after it, including new lines. 

I doubt it: dot (.) doesn't match newline sequences by default.

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

😨

#Region regEx Testing
    $testIn = "Anything?cd"

    wLog($testIn)
    $testOut = StringRegExpReplace($testIn,"\Q?\E[\s\S]*","")
    wLog(("\Q?\E[\s\S]* :" & $testOut))

    $testOut = StringRegExpReplace($testIn,"\?.*","")
    wLog(("\?.* :" & $testOut))


    $testOut = StringRegExpReplace($testIn,"\?[^\n]+","")
    wLog(("\?[^\n]+ :" & $testOut))

    Exit


#EndRegion

Now all methods are working just fine. 

1/11/2024 11:07:47 AM Anything?cd
1/11/2024 11:07:47 AM \Q?\E[\s\S]* :Anything
1/11/2024 11:07:47 AM \?.* :Anything
1/11/2024 11:07:47 AM \?[^\n]+ :Anything

I have no idea why the same code was giving different outputs the other day. That is a little scary.

Thank you for all of the help.

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...