Sign in to follow this  
Followers 0
MonsieurOUXX

Regular Expression to detect new lines in Edit Boxes?

4 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


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

Share this post


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

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  
Followers 0