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

Share this post


Link to post
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) ))

 


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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")

 


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

Share this post


Link to post
Share on other sites

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

(edit:  accidentally replied before finishing text)

Edited by lee321987

Share this post


Link to post
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") )

 

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
24 minutes ago, mikell said:

StringRegExp($test, '(?im)^C:\\\N*\S\N*$')

That did it!!  Thanks to both of you for helping!

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Chimp
      Hello
      if I have a string like in the example below,
      is there a regular expression that can surround any "string" (and only strings) within quotes?.
      The whole input string is a "constructor" to populate an array so even if an element contains more words (a phrase) it should be considered as a single word (Elton John should be considered a single word and as that quoted as "Elton John")
      for example
      the following string
      [[Elton John,Peter,Sally,123],[1 one 1,2,3,4 four 4]] should be transformed to this other string
      [["Elton John","Peter","Sally",123],["1 one 1",2,3,"4 four 4"]] Thanks for your help
      Here a small script to use as "guinea pig"
      #include <Array.au3> Local $aArray = [["Elton John", "Peter", "Sally", 123],["one 1", 2, 3, "4 four 4"]] MsgBox(0, "Result", _Array2Json($aArray)) Func _Array2Json($aArray) If (Not IsArray($aArray)) Or (UBound($aArray, 0) > 2) Then Return SetError(1, 0, '') Local $sOpening, $sClosing If UBound($aArray, 0) = 1 Then $sOpening = '[' $sClosing = ']' Else $sOpening = '[[' $sClosing = ']]' EndIf $sOutpt = $sOpening & _ArrayToString($aArray, ",", -1, -1, "],[") & $sClosing ; $sOutpt = ???? how to quote strings ???? Return $sOutpt EndFunc ;==>_Array2Json  
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By nend
      This is a program that I made to help my self learn better regular expressions.
      There are a lot of other programs/website with the similar functions.
      But the main advantage of this program is that you don't have to click a button after every changes.
      The program detected changes and react on it.
      Function:
      Match Match of arrays Match and replace Load source data from website Load source data from a website with GET/POST Load text data from file Clear fields Export and Import settings (you can finish the expression a other time, just export/import it) Cheat sheet Generate AutoIt code example code The source code is not difficult and I think most user will understand it.
      In the zip file there is a export files (reg back example), you can drag and drop this files on the gui to import it.
      Download Regex Toolkit Regex toolkit.zip  (Sourcode, example and compiled exe file)
      EDIT: Updated to version V1.2.0
      Changes are:
      Expand and collapse of the cheat sheet (Thanks to Melba23 for the Guiextender UDF) Usefull regular expressions websites links included in the program Text data update time EDIT: Updated to version V1.3.0
      Changes are:
       Automatic generate AutoIt code  Icons on the tab  Few minor bug fixes EDIT: Updated to version V1.4.0
      Changes are:
      Link to AutoIt regex helpfile If the regular expression has a error than the text becomes red Option Offset with Match and array of Matches Option Count with Match and replace Some small minor bug fixed EDIT: Updated to version V1.4.1
      Changes are:
      Small bug in "create AutoIt" code fixed EDIT: Updated to version V1.4.2
      Changes are:
      Small bug in "create AutoIt" code fixed Bug with website data  fixed 
      Regex toolkit.zip  (Sourcode, example and compiled exe file)
    • By therks
      I'm looking for a regex genius, cus I'm stumped when it comes to assertions.
      So what I have now, is this regular expression: ([^|=]+)=([^|]+)
      It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.
      Example:
      $vParamData = 'example=value|fruit=apple|phrase=Hello world' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; Result ; [0] => example ; [1] => value ; [2] => fruit ; [3] => apple ; [4] => phrase ; [5] => Hello world So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.
      ie:
      $vParamData = 'pipe test=this \| is a pipe|example=value' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; I'm getting this: ; [0] => pipe test ; [1] => this \ ; [2] => example ; [3] => value ; But I'd like a result like this: ; [0] => pipe test ; [1] => this \| is a pipe ; [2] => example ; [3] => value Is there some pattern that would accomplish this, or am I better off parsing it some other way?
    • By Chimp
      regex and iso escape sequences
      Hi, I would like to extract all ISO escape squences embedded in a string and separate them from the rest of the string, still keeping the information about their position, so that, for exemple, a string like this one (or even more complex):
      (the string could start with normal text or iso sequences)
       
      '\u001B[4mUnicorn\u001B[0m' should be 'transformed' in an array like this
      $a[0] = '\u001B[4m' ; first iso escape sequence $a[1] = 'Unicorn' ; normal text $a[2] = '\u001B[4m' ; second iso escape sequence ... and so on (note: the above escape sequence has 'control codes' marked as "\u001B' for the asc "esc" char for exemple and a similar notation is also used for other control chars, but in the real string to be parsed those control chars  are embedded  as a single byte with a value from 01 to 31). at this link (http://artscene.textfiles.com/ansi/) there are many example of real ANSI text files .
      searching on the web I've found some possible solutions that make use of regexp to achieve similar purpose, and above some others, the regexp pattern posted in the following link by kfir (https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python) seems to be able to catch a wider range of ISO escape sequences (not only color sequences), but my lack of skills on regexp, prevents me from evaluating and testing such patterns
      I would be very grateful if some regexp guru could come to my rescue...
      thanks everybody  for reading...
×
×
  • Create New...