Jump to content

String matching returns/breaks/feeds


Recommended Posts

I could use some assistance with string matching text that contains many returns. A good example is the source to this page. If you scroll down you will see there are many returns ala

Example:

<td>


<font color="green">FAST</font>

I think one problem is that I don't fully understand the difference between @CRLF, @CR, and @LF, how to tell what is being used, and which is typically used in any given circumstance. The help file doesn't really touch on that. I typically use @CRLF. I am also not sure if StringStripCR also strips @LF and @CRLF.

So suppose I want to match the previous string (specifically the word FAST), - I am going to have to use the line breaks/feeds/returns to assist in identification - both before the occurance of the word and after. Can someone help me with that?

Link to comment
Share on other sites

In my experience:

If it was written in Windows, then "line breaks" consist of a carriage return (@CR) and a line feed (@LF)

Otherwise a line break consists of just a line feed.

This is why (if you've seen this happen) sometimes you view the source to a webpage in Windows, it opens in Notepad, and it all appears to be on one line. Only the line feed character is present and Notepad relies on having the carriage return as well, or else it does not percieve a new line.

Edit:

Also, StringStripCR only strips carriage returns. So what you can do, is something like strip all the @CR from your source, then just use the @LF to try and find your text. Something like this:

$pageSource = "where you get the source for the page from"
$pageSource = StringStripCR($pageSource)
$compareLine = "<td>" & @LF & @LF & "<font color=""green"">FAST</font>"
StringInStr($pageSource, $compareLine)

Or however it is you want to do it.

Edited by Saunders
Link to comment
Share on other sites

thanks Saunders

i wrote a udf

$test = 'do' & @CRLF & 're' & @CR & 'me' & @LF & 'fa' & @CRLF & 'so' & @CR & 'la' & @LF & 'ti' & @CRLF & 'do'

MsgBox(1,"",_FindNewLine($test,'re','me'))

Func _FindNewLine( $nlString,$nlBefore,$nlAfter )

    $nlTypes = StringSplit(@CRLF & ',' & @CR & ',' & @LF,',')
    
    For $nlLoop = 1 to 3
        $nlSearch = StringInStr($nlString,$nlbefore & $nlTypes[$nlLoop] & $nlAfter)
        If $nlSearch <> 0 Then 
            Return $nlSearch
        EndIf
    Next
    
EndFunc

trying to think of an easy way to do the match even if there are more than one in a row..but it works for just one so far :)

Edited by Alterego
Link to comment
Share on other sites

woohoo! i did it!

$test = 'do' & @CRLF & 're' & @CR & @CR & @CR & @CR & 'me' & @LF & 'fa' & @CRLF & 'so' & @CR & 'la' & @LF & 'ti' & @CRLF & 'do'

MsgBox(1,"",_FindNewLine($test,'re',5,'me',5))

Func _FindNewLine( $nlString,$nlBefore,$nlMaxOccurBefore,$nlAfter,$nlMaxOccurAfter )
    $nlTypes = StringSplit(@CRLF & ',' & @CR & ',' & @LF,',')
    For $nlLoop = 1 to 3    
        For $nlLoop2 = 1 to $nlMaxOccurBefore + $nlMaxOccurAfter    
            $nlSearch = StringInStr($nlString,$nlbefore & $nlTypes[$nlLoop] & $nlAfter)
            If $nlSearch <> 0 Then 
                Return $nlSearch
            EndIf       
            $nlTypes[$nlLoop] = $nlTypes[$nlLoop] & $nlTypes[$nlLoop]       
        Next    
    Next    
EndFunc

:)

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