Jump to content
Sign in to follow this  
lordsocke

Regex cant find \n

Recommended Posts

lordsocke

Hey there, 

my code: 

Global $res = StringRegExp($text,"\n", 3)
If @error Then
    $nbOccurencesn = 0
Else
   $nbOccurencesn = UBound($res)
Endif
ConsoleWrite(@crlf&$nbOccurencesn&@crlf)

is not delivering any hits in the string:

"Which Reese Witherspoon movie\nhas gotten a Broadway musical\nadaptation?\nElection\nWalk the Line\nLegally Blonde\n"

any Ideas what im making wrong?

Share this post


Link to post
Share on other sites
iamtheky
Posted (edited)

looking for a literal, and not escaping the character also used for pattern matching

$text = "Which Reese Witherspoon movie\nhas gotten a Broadway musical\nadaptation?\nElection\nWalk the Line\nLegally Blonde\n"

Global $res = StringRegExp($text,"\\n", 3)
If @error Then
    $nbOccurencesn = 0
Else
   $nbOccurencesn = UBound($res)
Endif
ConsoleWrite(@crlf&$nbOccurencesn&@crlf)

 

from helpfile:

Quote
\n Represents "linefeed" (@LF, Chr(10)).

so thats what you were looking for and why a one line string would correctly return 0

Edited by iamtheky

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

Share this post


Link to post
Share on other sites
mikell

Anyway the real purpose is hopefully different, because such a simple thing doesn't need a regex  :mellow:

$text = "Which Reese Witherspoon movie\nhas gotten a Broadway musical\nadaptation?\nElection\nWalk the Line\nLegally Blonde\n"

$res = StringReplace($text,"\n", "\n")
;Global $res = StringRegExp($text,"\n", 3)
If not @extended Then ;@error Then
    $nbOccurencesn = 0
Else
   $nbOccurencesn = @extended ;UBound($res)
Endif
ConsoleWrite(@crlf&$nbOccurencesn&@crlf)

 

Share this post


Link to post
Share on other sites
lordsocke
On 20.4.2018 at 9:28 PM, mikell said:

Anyway the real purpose is hopefully different, because such a simple thing doesn't need a regex  :mellow:

$text = "Which Reese Witherspoon movie\nhas gotten a Broadway musical\nadaptation?\nElection\nWalk the Line\nLegally Blonde\n"

$res = StringReplace($text,"\n", "\n")
;Global $res = StringRegExp($text,"\n", 3)
If not @extended Then ;@error Then
    $nbOccurencesn = 0
Else
   $nbOccurencesn = @extended ;UBound($res)
Endif
ConsoleWrite(@crlf&$nbOccurencesn&@crlf)

 

But the Thing is, that I need to keep the \n inside the code. 

Share this post


Link to post
Share on other sites
lordsocke
On 20.4.2018 at 9:28 PM, mikell said:

Anyway the real purpose is hopefully different, because such a simple thing doesn't need a regex  :mellow:

$text = "Which Reese Witherspoon movie\nhas gotten a Broadway musical\nadaptation?\nElection\nWalk the Line\nLegally Blonde\n"

$res = StringReplace($text,"\n", "\n")
;Global $res = StringRegExp($text,"\n", 3)
If not @extended Then ;@error Then
    $nbOccurencesn = 0
Else
   $nbOccurencesn = @extended ;UBound($res)
Endif
ConsoleWrite(@crlf&$nbOccurencesn&@crlf)

 

thanks! 

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  

×