String Methods in Regex Find and Replace


120
views
0
3 months ago by
I'm trying to convert between variable naming conventions, from:

exampleVariable​


to:


example_variable​

I can find the relevant variables with the regex search "([a-z])([A-Z])", and use "\1_\2" to convert to "example_Variable", but to convert to the desired convention using re.sub() would require something like:

re.sub('([a-z])([A-Z])',lambda match: match.group(1)+"_"+match.group(2).lower(),'exampleVariable')​


However, simply placing the inline function in Wing's replace line doesn't achieve the desired effect. Is there an alternative way to accomplish what I'm looking to do?

Community: Wing Python IDE

1 Answer


2
3 months ago by
In Wing Pro there are "Symbol To *" refactoring operations for converting between UpperCamelCase, lowerCamelCase, under_score_name, and UNDER_SCORE_NAME forms.  To use that, click on the symbol an use the items in the Refactor menu or right click to get a menu that includes a Refactor group.  This will change all points of use of that symbol, whether in the current file or not so is probably a lot easier to use than search/replace.

If you do need to write code for the conversion, you'ld have to do it via the scripting API which is documented at http://wingware.com/doc/scripting probably by operating directory on the CAPIDocument.
Thanks, that'll work great!
written 3 months ago by Jordan Smart  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »