Jump to content

Regular Expression to detect new lines in Edit Boxes?


Recommended Posts

Hi,

My question is related both to GUI and Regular Expressions, so I didn't really know where to post it. I decided to post it here.

I'm trying to use "StringRegExpReplace" to change the text contained in an Edit Box (created using GUICtrlCreateEdit).

The text in the box is made of several lines. Therefore, each line finishes with "@CRLF"

Now, I'm trying to , let's say, replace the first character of each line with "@". It's just a simple example. Even if the line is empty (contains only @CRLF). My goal is to replace the first character of each line with something else, and then to paste back the new text into the Edit box.

I don't know what regular expression to use. In particular, I'm not sure what character I should use in the regular expression to detect the end of the line. I've tried \r but I'm not sure if it's the proper equivalent to @CRLF.

You're going to ask : "OK, show us your current RegExp", but to be honest it's crap and I'm pretty sure it's random so I'd better trust you.

Link to comment
Share on other sites

You're going to ask : "OK, show us your current RegExp", but to be honest it's crap

That's really no excuse for not posting it.

Most members won't rip you to shreds over RegExp stuff as it can be pretty tough to wrap your head around sometimes.

Anyway, here's a few hints:

  • you're going to need something that identifies either a carriage return (the "\r" you already have) and a line feed, OR something that identifies the start of a line
  • you're going to need to capture everything from that point to the next crlf or start of line
  • you need to StringRegExpReplace that capture with the "@" followed by the captured string
  • oh yeah, and you'll need to turn on the "multiline" or "global" switch

Now show us what you've got >_<

Edit: by the way, what if there's no crlf on the last line? Do you want it ignored or prepended as well?

Edited by ResNullius
Link to comment
Share on other sites

OK, so I've answered the first question myself: @CRLF is equivalent to \r\n in a regular expression.

Now, I still have issues replacing only *part* of what has been detected by the regular expression. I don't know if it's possible at all, due to the nature of StringRegExpReplace: maybe I should use groups but I don't understand that very well yet.

Instead, I'm using StringRegExp and I'm changing the results manually.

For example, with the code below I apply a specific treatment on each line.

(Note : I haven't added the processing of fancy cases like "no \r\n at the end", etc. That will come later)

$result=""
$array = StringRegExp($text, ".*\r\n", 3)
    
    for $i = 0 to UBound($array) - 1
        $result = $result&ProcessLine($array[$i])
    Next
return $result
Link to comment
Share on other sites

OK, so I've answered the first question myself: @CRLF is equivalent to \r\n in a regular expression.

Good! Always encouraging to see some effort >_<

How about this?

$string = "" & @CRLF
$string &= "Line before this is empty, so is the line after" & @CRLF & @CRLF
$string &= "Another line" & @CRLF & "And another line, with another empty line following" & @CRLF & @CRLF
$string &= "Last line, but no CRLF"
MsgBox(0,"String Before",$string)
$string = StringRegExpReplace($string, "(?m)^.(.*?)","@\1")
MsgBox(0,"String After",$string)

It does what you requested in your first post and accounts for no @CRLF on a line.

However, I see now you mention

Now, I still have issues replacing only *part* of what has been detected by the regular expression

What do you mean by *part* of what has been detected by the RegExp?

Or do you just mean the first character on each line

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