Author Topic: How do regular expressions work?  (Read 10376 times)

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
How do regular expressions work?
« on: July 05, 2007, 09:19:02 pm »
I've been trying to replace some strings in CB using regular expressions, but i can't get it to work. Can you provide me some examples?

Also, how do the "advanced regular expressions" and "use POSIX-style..." options alter the regex search?

Please help! :(

Thanks :)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5529
Re: How do regular expressions work?
« Reply #1 on: July 05, 2007, 09:40:25 pm »
that's hard to explain just by forum, pff, tell us one you want to have, if it i not to complicated we can give the answer and explain that one ?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: How do regular expressions work?
« Reply #2 on: July 05, 2007, 10:15:21 pm »
..are you aware of the regexp testbed plugin in C::B?
This is helpful for testing at least...
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline dmoore

  • Developer
  • Lives here!
  • *****
  • Posts: 1576
Re: How do regular expressions work?
« Reply #3 on: July 05, 2007, 10:20:22 pm »
Also, how do the "advanced regular expressions" and "use POSIX-style..." options alter the regex search?

blame me for that :)

* With both options unchecked, you get the default regex engine built into scintilla (a pretty weak implementation IMO).  Regex "groups" are enclosed in escaped parantheses \( and \). this is helpful when you want to search for text that actually contains "(" or ")" characters and don't use groups very often (e.g. when working with LISP code).

* with POSIX checked (but advanced unchecked), regex groups are enclosed in ( and ), and to find the literal "(" and ")" chars you need to escape them \( \)

* Checking advanced regular expressions overrides scintilla RE and you get wxWidgets more powerful ARE syntax (link in my sig). The POSIX checkbox is irrelevant (I don't think AREs offer the choice of escaped vs unescaped parentheses). So, ideally when advanced is check, POSIX-style should just be greyed out.

The tradeoff between the scinitlla RE and wxWidgets is naturally a choice between a speedy native implementation in scintilla vs a powerful but slower ARE implementation (slower mostly because of the way I implemented the find and replace code).

As a side note: The ARE engine used by wxWidgets was written by Henry Spencer and distributed under a virtually unrestricted license other than the need to give him credit (and note any alterations to the code). Bizarrely, this was a sufficient disincentive for the Scintilla guy(s) to natively include AREs. It's hard to tell whether that was out of ignorance or arrogance...
« Last Edit: July 05, 2007, 10:53:35 pm by dmoore »

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: How do regular expressions work?
« Reply #4 on: July 05, 2007, 10:46:49 pm »
Thanks! I managed to use the regexes now :)

Offline End User

  • Single posting newcomer
  • *
  • Posts: 2
Re: How do regular expressions work?
« Reply #5 on: August 31, 2010, 10:19:15 am »
that's hard to explain just by forum, pff, tell us one you want to have, if it i not to complicated we can give the answer and explain that one ?

my situation:
i want to replace
Code
switch (expression)
by
Code
switch ( expression )

i managed to find (if|switch|for|while) \((.*?)\)
but i cannot imagine what token C::B uses for replacement

it's neither
$1 ( $2 )
nor
%1 ( $2 )
not even
\\1 ( \\2 )

what is a replacement template here?

Offline End User

  • Single posting newcomer
  • *
  • Posts: 2
Re: How do regular expressions work?
« Reply #6 on: August 31, 2010, 10:40:17 am »
may be you will consult me how to do it from linux command line? all *.c files at once
thanks in advance
« Last Edit: August 31, 2010, 10:42:54 am by End User »