Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Confused by regular expressions  (Read 1534 times)

mainiac

  • Bay Watcher
  • Na vazeal kwah-kai
    • View Profile
Confused by regular expressions
« on: October 20, 2011, 10:28:10 pm »

I'm trying to do some pretty basic regular expression syntax.  I want to do a very extensive search and replace operation in notepad++ using regular expressions.  But I can't make the darn thing accept a newline as part of the expression.  As far as I can tell  "\n\r" should do what I want but it isn't working in notepad++ or scintilla.  To give an example, I want an expression that would match:

thisisastring
andthisisanother

I think this should just be "thisisastring\n\rthisisanother" but that doesn't seem to work.  Can anybody give me a hand?

Edit: turns out that despite what a bunch of people on the internet implied, notepad++ and scintilla don't support multiline regular expressions.  >:(

So, new topic.  Anybody have any recommendations of good freeware text editors besides notepad++ and scintilla?
« Last Edit: October 20, 2011, 10:58:55 pm by mainiac »
Logged
Ancient Babylonian god of RAEG
--------------
[CAN_INTERNET]
[PREFSTRING:google]
"Don't tell me what you value. Show me your budget and I will tell you what you value"
« Last Edit: February 10, 1988, 03:27:23 pm by UR MOM »
mainiac is always a little sarcastic, at least.

timotheus

  • Bay Watcher
  • New to Dwarf Fortress.. Long time ASCII gamer..
    • View Profile
    • Home
Re: Confused by regular expressions
« Reply #1 on: October 21, 2011, 11:34:08 am »

I am really liking http://www.sublimetext.com/. It never expires, though I am going to purchase it.

It has featuers like intelligent new lines, so if I'm making a function:

Code: [Select]
function foo()
{

When I hit enter after the curly bracket, I get this:

Code: [Select]
function foo()
{
    |  <- cursor here
}

It can autocomplete, can Alt + . to close tags, lots of other shortcuts. Supports split view, projects, etc.
Logged
twitter: @timotheus
email: tim@hithlonde.com
xmpp: tim@hithlonde.com

Starver

  • Bay Watcher
    • View Profile
Re: Confused by regular expressions
« Reply #2 on: October 21, 2011, 12:01:54 pm »

I know you've found it won't work anyway, in the packages you're using, but...

"\n\r" should do what I want

Are you sure?  There's a difference between Unix and MS systems, in that the 0x0D 0x0A (IIRC[1]) of DOS-and-onwards systems is CarriageReturn+LineFeed, as opposed to just a single LineFeed character being used in Unix.  But DOS-implemented Perl, for example, will treat "\n" as the two characters, and Unix-implemented Perl will treat it as the one.  I'd expect DOS (and Windows) editors implementing the multiline-capable regexping that you're after to do the same.

(Although I remember microEmacs needing to specific about the two control characters, when I used that in the Early '90s.  That wasn't the same regexp notation, however, so probably doesn't count.  However, if you can get a suitable variant of Emacs and get around to learning all the controlling escape-sequences you need, that might do what you want.  I'll admit that I'm not sure I can remember them all, so you might be on your own if there isn't someone out there currently an Emacs buff.  Or, alternatively, there might be a vi one wanting to drag you into their own little Cabal. :) )

Also, I believe it should be in the order "\r\n".  Again, ICBW, but it looks more correct at first glance.  Lazy me having been lazily using "\n" alone on all the different platforms I've used recently, knowing that (for my purposes, at least) it does what I want it to do!


[1] Long time since I hex edited them, so I'm very much open to being corrected. :)
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Confused by regular expressions
« Reply #3 on: October 21, 2011, 12:08:16 pm »

Actually \n works on word, notepad++ and wordpad (as well as many other editors, including emacs IIRC), but not in notepad and some other simple text editors, so that may be why you never noticed a difference.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Confused by regular expressions
« Reply #4 on: October 21, 2011, 06:42:55 pm »

Sed is nice, I use it to clean files up every now and then. There's also a Windows version (along with a bunch of other Unix utilities.).

Also, I'm pretty sure it's "\r\n", but it's been a while since I tried messing with things on Windows.
Logged

eggrock

  • Bay Watcher
    • View Profile
Re: Confused by regular expressions
« Reply #5 on: October 25, 2011, 01:15:43 pm »

gVim

/this\nthat

will find

this
that

[edit] It's vi for Windows and there's a learning curve for vi, assuming you don't know it already
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Confused by regular expressions
« Reply #6 on: October 25, 2011, 03:11:09 pm »

« Last Edit: October 25, 2011, 03:17:01 pm by ILikePie »
Logged

Twiggie

  • Bay Watcher
    • View Profile
Re: Confused by regular expressions
« Reply #7 on: October 25, 2011, 03:14:59 pm »

wait, can't you just use a question mark to make the carriage return optional?
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Confused by regular expressions
« Reply #8 on: October 31, 2011, 03:02:29 pm »

BTW, textpad supports multiline regexps.  :)
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: Confused by regular expressions
« Reply #9 on: October 31, 2011, 08:56:18 pm »

First off, yes, on Windows it should be \r\n, not \n\r.  Assuming Notepad++ doesn't silently translate to/from naked \n, but that seems unlikely.

It's possible Notepad++ just doesn't support escape codes in its regular expressions.  You might be able to get away with replacing \r\n with .. (two dot operators).  That's not semantically perfect, but it might be good enough for your purposes.
Logged
Through pain, I find wisdom.