Jump to content

Regex cant find \n


Recommended Posts

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?

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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! 

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