Jump to content

Recommended Posts

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

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

 

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

Link to comment
Share on other sites

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")

 

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

Link to comment
Share on other sites

This one returns the n'th non-empty last line   :)

$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

$n = -2  ; number of the line to get
Msgbox(0,"", StringRegExpReplace($test, '(?ms).*?(^\N*\S\N*$)\s*(?:(?1)\s*){0,' & -$n-1 & '}\z', "$1") )

 

Link to comment
Share on other sites

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

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