Jump to content

RegEx help


Recommended Posts

So I have a program that has morphed a bit.. And StringRegExpReplace that used to work before, now no longer does because the lines in the file I'm working on have either have a space after them or a space then word after them.. The file now has.. IE.    (?m)/*$ matches /'s on at the end of a line.. I thought that  changing $ to \b would work, but that makes things worse.. help? My function is below.

1711848-002A 300.1_W NO3/NO2/ J-FLAG
1711855-001C 300.1_W NO3/NO2/CL/ <--- Note there is a space after the slash
1711849-001B 300.1_W NO3/NO2 J-FLAG

What I want them to look like:

1711848-002A 300.1_W NO3/NO2 J-FLAG
1711855-001C 300.1_W NO3/NO2/CL 
1711849-001B 300.1_W NO3/NO2 J-FLAG

 

Func ProcessAndCleanFiles($filetocleanup) ; Basically deletes the /'s at the end of a sample analytes
    $Whattowrite = FileRead($filetocleanup)
    MsgBox(0, 0, "before: " & $Whattowrite)
    $Whattowrite = StringRegExpReplace(FileRead($filetocleanup), "(?m)/*$", "")
    MsgBox(0, 0, "after: " & $Whattowrite)
    
    FileWrite($filetocleanup, $Whattowrite)
    FileClose($hFileOpen)

EndFunc   ;==>ProcessAndCleanFiles

 

Link to comment
Share on other sites

closer....maybe.

$string = "1711848-002A 300.1_W NO3/NO2 J-FLAG" & @CRLF & _
"1711855-001C 300.1_W NO3/NO2/CL/ " & @CRLF & _ ;<--- Note there is a space after the slash
"1711849-001B 300.1_W NO3/NO2/ J-FLAG"


    MsgBox(0, 0, "before: " & @CRLF & $string)
    $string = StringRegExpReplace($string, "(?m)(.*)/(\s.*$)", "$1$2")
    MsgBox(0, 0, "after: " & @CRLF & $string)

 

Edited by iamtheky

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

Link to comment
Share on other sites

Am I correct in my understanding? 

-----

(?m)(.*)/(\s.*$)   breaks down to

1) (?m) Search every line seperately

2) (.*) Any Charecter, and any number of that charecter.. aka.. EverythingAnything before the /

3) / is just the / were looking for

4) (\s.*$)  Matches any whitespace/anycharanynumberofchars/endofline

------

So if we look at 1711848-002A 300.1_W NO3/NO2/ J-FLAG

 (.*) covers to and including 1711848-002A 300.1_W NO3/NO2

Then (.*)/ covers to and including 1711848-002A 300.1_W NO3/NO2/

Then (?m)(.*)/(\s.*$)  covers everything after the / basically so any spaces or J-FLAGS. 

----

I think I get it.. Hopefully next time I can reproduce it? :) Thanks for your help.

Link to comment
Share on other sites

that's about right, i would summarize:

?per line, take whatever you can leaving for sure a forward slash that is followed by a space then maybe some other stuff, but definitely a line ending character; also capture that stuff after that forward slash.  Then replace that string with the first capture $1 and the second capture $2 smashed together.

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

Link to comment
Share on other sites

From now on I stringops while you regex, the other way feels weird.

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

Link to comment
Share on other sites

Yes, please do!

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