topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 23, 2024, 4:37 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: SciTE regex replacement  (Read 5009 times)

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
SciTE regex replacement
« on: August 03, 2012, 05:21 PM »
I can't seem to get SciTE to regex replace for me. All I'm trying to do is stick a '_' in front of every line where the start of the line has [a-zA-Z].

If I use ^[a-zA-Z]   in the search field, it pics it up. But no matter what I put in the Replace field after the underscore, it replaces the first character with the underscore instead of sticking the underscore in front. I think the '^' denoting start of line is messing me up.

Seems like it should be something simple that's done all the time.  Damned if I can figure it out though.

Here's an example.  I have functions like

MyFunc(param)
SomeFunc(param)
ThisFunc(param)


After the replacement I should have

_MyFunc(param)
_SomeFunc(param)
_ThisFunc(param)

Why is it so difficult?



jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #1 on: August 03, 2012, 06:03 PM »
If you try to replace "^[a-zA-Z]" with an underscore, it'll always match the first character, it's supposed to. Have you tried using only "^" (which denotes only the start of the line)?

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #2 on: August 03, 2012, 06:37 PM »
Ok, a more accurate representation of the problem is this

funcName(param)
{
   ; some code
}

anotherFuncName(param1, param2)
{
  ; some more code
}

thirdFunc()
{
  ; yadda yadda
}

I only want the lines that start with [a-zA-Z] to start with _(funcnameline)

and not end up with
_funcName(param)
_{
_ ; some code
_}




jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #3 on: August 03, 2012, 06:54 PM »
Right, that makes sense. Then you need the expression to match the first character in the way you were doing before.
One way to solve the problem is to use:
regex: ^([a-Z])
replace: _$$1
This means: match the first character, replace with "_" concatenated with the character that was matched.
Depending on the regex engine, the "(" can also be "\(", and the "$$1" can be "$1" or "\1". (yep, it's annoying that not everybody follows the same syntaxes).

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #4 on: August 03, 2012, 07:42 PM »
Thanks for your replies. In the meantime I got a response on ahk forum. Awk I could do a bit, but regex is not my strength. Easier just to write an app to read the input and spit out the output than figure this out.  Here's the solution Lexicos posted:

http://www.autohotke...amp;p=556410#p556399


Edvard

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,017
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #5 on: August 04, 2012, 10:06 PM »
(yep, it's annoying that not everybody follows the same syntaxes).

Yep, in Textpad 5, you use the '&' symbol to spit out what you matched, so you would replace like so:

Find:      ^[a-Z]
Replace: _&

:shrug:

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: SciTE regex replacement
« Reply #6 on: August 05, 2012, 12:03 AM »
The main thing messing me up was putting the start of line symbol inside the pattern. I found PsPad makes it a bit easier than SciTE. This works in PsPad

find
^([a-zA-Z])

replace
_$1

and

func(param)
{
  jive()
}

func2(param2)
{
  jive2()
}

is magically transformed into

_func(param)
{
  jive()
}

_func2(param2)
{
  jive2()
}

It's a lot less confusing without the backslashes.