Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable regular expression case changing #12185

Closed
elkebirmed opened this issue Sep 17, 2016 · 58 comments
Closed

Enable regular expression case changing #12185

elkebirmed opened this issue Sep 17, 2016 · 58 comments
Assignees
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities search-replace Search and replace issues verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@elkebirmed
Copy link

  • VSCode Version: 1.4.0
  • OS Version: Ubuntu 16.04

Steps to Reproduce:

  1. Ctrl+H
  2. Find camelCase strings ([a-z])([A-Z])
  3. Change it to lowercase $1-\L$1\E

expected fooBar to be foo-bar

@sandy081 sandy081 added the feature-request Request for new features or functionality label Oct 3, 2016
@sandy081
Copy link
Member

sandy081 commented Oct 3, 2016

@elkebirmed May I know what are the standards/definitions for using \L and \E in your example?

@elkebirmed
Copy link
Author

@sandy081 Take a look

@elkebirmed
Copy link
Author

Let's say that we've this string:

Hello World

Capturing it with this RegEx: (Hello) (World)

Replacement is: \U1 \L2 will give us -> HELLO world

\U1 = $1 to Uppercase
\L2 = $2 to Lowercase

@sandy081 sandy081 added search Search widget and operation issues editor-find Editor find operations editor labels Oct 4, 2016
@sandy081
Copy link
Member

sandy081 commented Oct 4, 2016

@elkebirmed Thanks for the documentation. This is cool to have. This is beyond the scope of what is currently supported by javascript.

We need to come up with our own advanced replace engine to support these cases.

Thanks

@sandy081 sandy081 added this to the Backlog milestone Oct 4, 2016
@elkebirmed
Copy link
Author

You're welcome @sandy081

@kdar
Copy link

kdar commented Nov 27, 2017

It would be nice to have a way to supply a javascript function to the capture groups, so we can choose how to transform them and do more things than just upper/lower case. Since javascript already provides this, we just need an interface for it.

"some_VAR".replace(/([a-z]+)_([A-Z]+)/, function(match, p1, p2) { 
  return [p1.toUpperCase(), p2.toLowerCase()].join("_");
})
// "SOME_var"

@sandy081 sandy081 added search-replace Search and replace issues and removed editor-find Editor find operations search Search widget and operation issues labels Dec 4, 2017
@alystair
Copy link

alystair commented Jun 9, 2018

Ran into this today, seems regex replacement still isn't quite there yet.

@ghost
Copy link

ghost commented Jun 11, 2018

Ran into this today. Anyone know of an online tool which supports case conversion in regex replacement - just to save me having to install SublimeText just for this task, while waiting for VSCode to catch up?

@jonbito
Copy link

jonbito commented Jun 11, 2018

@BrianLowe Notepad++ will do what you need.

@ghost
Copy link

ghost commented Jun 12, 2018

@retnull Thanks. Notepad++ does the casing transformation. (Now I just need to work up a suitably selective expression and corresponding substitution to complete my task.

@freman
Copy link

freman commented Jun 26, 2018

I miss this from sublime :(

@mesqueeb
Copy link

mesqueeb commented Jul 9, 2018

+1 for this functionality! I'm currently manually converting case on 100+ results in 50+ files... lol

@R3V1Z3
Copy link

R3V1Z3 commented Aug 20, 2018

As a temporary workaround, you can use a workflow like so:

  • Press CTRL-H (⌥⌘F on Mac).
  • Press ALT-R (⌥⌘R on Mac).
  • Type _([a-zA-Z]).
  • Press TAB and type $1.
  • Press ALT-ENTER (⌥ENTER on Mac).
  • Press F1 and type upper, then press ENTER.
  • Press CTRL-ALT-ENTER (⌥⌘ENTER on Mac).

A bit of explanation added here:
https://gist.github.com/2a271f6440f1ac7e028df55e94035e40

@jpike88
Copy link

jpike88 commented Aug 21, 2018

@Ugotsta that workaround makes sense only to Windows users.

@jpike88
Copy link

jpike88 commented Aug 21, 2018

A more general workaround

  • Open find and enter the regex _[A-Za-z]
  • Open from top menu bar, Selection > Select all occurrences
  • Open command menu (⇧⌘P on Mac, Ctrl+Shift+P on Windows/Linux), then type 'Transform to Uppercase', hit enter.
  • Then press Left Arrow once, then Backspace once

@vbullinger
Copy link

This issue is over two years old and sounds really easy to fix and useful. Can we get an update or roadmap on this, please?

@ashclarke
Copy link

ashclarke commented Dec 6, 2018

Is this possible, i.e. worth roadmapping, now that PCRE2 is an option, as of v1.29.0?

PCRE2's Replacement String Case Conversion
PCRE2 supports case conversion in replacement strings when using PCRE2_SUBSTITUTE_EXTENDED. \U converts everything that follows to uppercase. \L converts everything that follows to lowercase. \u converts the next character to uppercase. \l converts the next character to lowercase. \E turns off case conversion. As in Perl, the case conversion affects both literal text in your replacement string and the text inserted by backreferences.

Unlike in Perl, in PCRE2 \U, \L, \u, and \l all stop any preceding case conversion. So you cannot combine \L and \u, for example, to make the first character uppercase and the remainder lowercase. \L\u makes the first character uppercase and leaves the rest unchanged, just like \u. \u\L makes all characters lowercase, just like \L.

In PCRE2, case conversion runs through conditionals. Any case conversion in effect before the conditional also applies to the conditional. If the conditional contains its own case conversion escapes in the part of the conditional that is actually used, then those remain in effect after the conditional. So you could use ${1:+\U:\L}${2} to insert the text matched by the second capturing group in uppercase if the first group participated, and in lowercase if it didn't.

@nemchik
Copy link

nemchik commented Jul 21, 2019

Would you be able to provide a link?

@briancrink
Copy link

Would you be able to provide a link?

Text Transformer For VS Code
uppercase
change case
TextTransform

I don't remember remember which extension I use or if it's a feature now, the command I use from the command palette is 'Transform to'

@nemchik
Copy link

nemchik commented Jul 21, 2019

Those extensions don't quite meet the need. Vscode already has case transformation natively, but something you can't do is regex find across multiple files and replace the found items with upper or lower case all at once.

@freman
Copy link

freman commented Jul 30, 2019

Eg:

Search: \s+private final (\w+) (\w+);
Replace \t\u$2 \l$1 `json:"$2"`

Use case: Converting structures to go

@liudonghua123
Copy link

I like this awesome feature, and sometimes I need to use this feature like replace all the tag name of html to lowercase if possible.

@inoyakaigor
Copy link

Is any updates for this issue?

@sholtomaud
Copy link

That MS are not attending to this basic and important feature suggests that VSCode may be deprecated soon in favor of some other code editor. Shame, I quite liked VSCode.

@janosh
Copy link

janosh commented Feb 24, 2020

suggests that VSCode may be deprecated soon in favor of some other code editor.

Somehow I doubt it. 😉

@roblourens
Copy link
Member

We could take a PR for this if anyone wants to work on it. I think we would want something basically consistent with another implementation: https://www.regular-expressions.info/replacecase.html but we can only support it for 'replace', not for searching with backreferences like Perl apparently supports. If someone wants to work on it, please let's talk about how it would work first 😁

@roblourens roblourens added the help wanted Issues identified as good community contribution opportunities label Feb 25, 2020
@sholtomaud
Copy link

sholtomaud commented Feb 25, 2020 via email

@matheo
Copy link

matheo commented Feb 26, 2020

Alt+Enter + F1 worked on my Ubuntu. Thanks to #12185 (comment)

@blueray453
Copy link

blueray453 commented Apr 9, 2020

It seems like not being able to change case using regex is an upstream issue. I hope by going through chmln/sd#72 (comment) & BurntSushi/ripgrep#1546 (comment) you will be able to understand what is the problem and what might be the solution.

@roblourens
Copy link
Member

It's not upstream, replacement is implemented in VS Code, not ripgrep.

@blueray453
Copy link

@roblourens which regex flavour are you using for replacement?

@irridia
Copy link
Contributor

irridia commented Apr 25, 2020

@roblorens Search and replace is implemented differently in at least three places in VSCode.

As @blueray453 mentions, the global S&R seems to be implemented with ripgrep, which only "accidentally" supports replace and has no plans to add replacement features. So even if someone wanted to contribute the time to ripgrep, it likely wouldn't get merged. And the cost of revamping and unifying VSCode's various search implementations is a bit larger than most third-party PRs. And I'm not holding my breath. ;)

That said, I do have a draft PR #96128 (!) that I may spend more time on if there's interest (I'm not a Node guy). In my case I had mostly a single very long file I wanted to S&R, so "at least I'm okay". ;-)

@roblourens
Copy link
Member

Merged this for the find widget - work to do this for search is still ongoing.

@roblourens roblourens added the verification-needed Verification of issue is requested label Jun 30, 2020
@lramos15 lramos15 added the verified Verification succeeded label Jul 1, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities search-replace Search and replace issues verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests