1

Is there a way to use perl-style regular expressions to match a character and replace it with it's upper/lower case equivalent without relying on another language (php, javascript, whatever)?

Modifying string case would be handy to be able to do in the VI editor and Notepad++ and PHP, etc instead of having a different approach for every context.

Probably not, huh..

doub1ejack
  • 10,627
  • 20
  • 66
  • 125
  • 1
    I'm afraid that pure regex can't do such thing. – Toto Feb 06 '13 at 15:19
  • Yes, [in Notepad++, you may](https://stackoverflow.com/questions/31952353/notepad-capitalize-every-first-letter-of-every-word). Also, it is possible in many R builds, in Boost regex in general. – Wiktor Stribiżew Sep 21 '17 at 08:16
  • @WiktorStribiżew This question is not a duplicate of the post you linked to. This question is specifically about _the capabilities of **regex**_. The post you linked to is looking at non-regex alternatives like EditPad & Notepad++. – doub1ejack Sep 29 '17 at 16:16

3 Answers3

2

Like you said, "Probably not."

Regexes aren't meant to modify strings, only to search / match against them. You will always need some kind of "external" language or program to make modifications or otherwise handle the regex' output.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
1

from my understanding, the answer is False.

regex defines the syntax, how to match text. but how to do further operations on the matched text, that's the job of concreted implementations. like substitution function in python/java/perl/vim/sed/awk/... some tool doesn't provide that, e.g. grep.

same for case changing.

If you think about it, the method/function of substitution are different among those language/tools

awk: sub/gsub
sed: s/../../
vim: s/../../
python: re.sub()
java: replaceAll(..) ...
...

if regex supported it, why no standard function?

Kent
  • 189,393
  • 32
  • 233
  • 301
  • All the functions you gave an example off just use the regex' output as a list of items to rearch & replace. That's got nothing to do with the regex itself. Regexes don't "have" to support those functions, since the regexes have no knowledge of how they're being used. – Cerbrus Feb 06 '13 at 15:26
0

regular expressions are used for matching, your tool (Vi, Notepad++, PHP) is doing the substitution.

X Tian
  • 766
  • 7
  • 13