Jump to content

Recommended Posts

Posted (edited)

Hello, I'm trying to match the second to last line of this:

foo
C:\
foobar
foobar x
C:\temp\dir
Last line with chars

Here's my code:

$test = 'foo' & @CRLF
$test &= 'C:\' & @CRLF
$test &= 'foobar' & @CRLF
$test &= 'hello' & @CRLF
$test &= 'C:\temp\dir' & @CRLF
$test &= 'Last line with chars' & @CRLF
$test &= @CRLF
$test &= @CRLF
$result = StringRegExp($test, '(?m)^C:\\.*$Last.*')
MsgBox(0, '', $result)

I'm trying to match line "C:\temp\dir".  Anyone have any ideas?

Edited by lee321987
Posted

In addition to modifying the expression, if you want the return you need to use the correct flag on stringregexp.  These efforts are only better in that they return the desired result, they are not optimal. 

one regex, one not:

$test = 'foo' & @CRLF
$test &= 'C:\' & @CRLF
$test &= 'foobar' & @CRLF
$test &= 'hello' & @CRLF
$test &= 'C:\temp\dir' & @CRLF
$test &= 'Last line with chars' & @CRLF
$test &= @CRLF
$test &= @CRLF


msgbox(0, '' , stringregexp(StringStripWS($test , 2) ,  @CRLF & "(.*?)" & @CRLF & ".*?\z" , 3)[0])

;~ msgbox(0,'' , stringmid(stringstripws($test , 2) , stringinstr(stringstripws($test , 2) , @LF , 0 , -2) , stringinstr(stringstripws($test , 2) , @LF , 0 , -1) - stringinstr(stringstripws($test , 2) , @LF , 0 , -2) ))

 

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

Posted

Thanks for the help, but (I'm sorry) I should have said that "C:\temp\dir" won't always be the second-to-last line.

I need to match the string - not just "the second to last line".

Posted

so you just need to know if that string is present or not?

$test = 'foo' & @CRLF
$test &= 'C:\' & @CRLF
$test &= 'foobar' & @CRLF
$test &= 'hello' & @CRLF
$test &= 'C:\temp\dir' & @CRLF
$test &= 'Last line with chars' & @CRLF
$test &= @CRLF
$test &= @CRLF



msgbox(0, '' , StringInStr($test , 'C:\temp\dir') > 1 ? "Match" : "No Match")

 

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

Posted (edited)

Oops.  I meant -- yes, but I want to know how to do it with regex.

(edit:  accidentally replied before finishing text)

Edited by lee321987
Posted

Ok I'm REALLY sorry, I need to clarify:

I won't always know what line "C:\temp\dir" is on.

I just need to make sure there is a line that starts with "C:\" and it has some text after "C:\" before the next newline (because the line "C:\" will always be present).

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
×
×
  • Create New...