Jump to content

Regular Expressions - Strings


atomman
 Share

Recommended Posts

I can do very simple tasks with regular expressions, but beyond that i'm lost.

I need to detect all kinds of returns in a string copied from the windows clipboard (LF, CR, CRLF), then replace them with some html tags and new returns.

If there's multiple returns together, i don't want empty tags though. So...

"<p>i am a @CRLF string</p>"

and...

"<p>i am a @CRLF @CRLF string</p>"

needs to end up as...

"'<p>i am a</p>' & @CRLF & '<p>string</p>'"

Link to comment
Share on other sites

$string = "Line 1 " & @CRLF & @CRLF & "Line2" & @CRLF & "Line 3" & @LF & @CRLF & "Line 4" & @CR & "Line 5"
$result = StringRegExpReplace($string, "(" & @CRLF & "|" & @CR & "|" & @LF & ")","</p><p>")
MsgBox(0,"",$result)

Seems to work pretty good! If there's multiple line feeds back to back (see code), i get empty tags (<p></p>)

Unless you think i should do it differently, i'll just StringReplace("<p></p>", "") what is returned from above.

And thanks! This solves several issues for me.

Link to comment
Share on other sites

nothing :D

If you have say, 3 line feeds, back to back, the code will return multiple empty tags (<p></p><p></p><p></p>) since it's replacing every line feed with the tags. I want to simply discard them -- or -- remove all but the first line feed, then replace the line feeds with the tags. In other words, if there's 2+ line feeds back to back, remove the first, then replace the remaining with the the tags. I would assume it's either that, or run another StringReplace() on what was returned to strip empty tags.

Edited by atomman
Link to comment
Share on other sites

Shoot, I got your back.

$string = "Line 1 " & @CRLF & "Line2" & @CRLF & @CRLF & "Line 3" & @LF & "Line 4" & @CR & "Line 5"
$result = StringRegExpReplace(StringStripWS($string, 4), "(" & @CRLF & "|" & @CR & "|" & @LF & ")","

")
MsgBox(0,"",$result)

StringStripWS -

flag 4 = strip double (or more) spaces between words

Link to comment
Share on other sites

Shoot, I got your back.

$string = "Line 1 " & @CRLF & "Line2" & @CRLF & @CRLF & "Line 3" & @LF & "Line 4" & @CR & "Line 5"
$result = StringRegExpReplace(StringStripWS($string, 4), "(" & @CRLF & "|" & @CR & "|" & @LF & ")","

")
MsgBox(0,"",$result)

StringStripWS -

flag 4 = strip double (or more) spaces between words

But what if he wants to keep double (or more) spaces between words? He just wants to strip double line breaks.

I mean, your code does what he wants, yes, but it's also doing something more.

It turns this:

"This is a string.  With double" & @CRLF & @CRLF & "spacing.  After each period." & @CRLF & @CRLF & "And two double.  Line breaks."

Into this:

"This is a string. With double" & @CRLF & "spacing. After each period." & @CRLF & "And two double. Line breaks."

When he might want this:

"This is a string.  With double" & @CRLF & "spacing.  After each period." & @CRLF & "And two double.  Line breaks."

Or maybe not, just thought I'd mention it in case.

Link to comment
Share on other sites

Hi Saunders

His code removes all line feeds... and spaces. That's what i needed to do. In your example (2nd line), you show the output as having line feeds, but they're removed.

So far it seems to do exactly what i needed -- well, what it doesn't do i'm capable of making it do. I just needed a start. I have a tough time with reg-exp.

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