开发者

Regular expression for finding non-breaking string names in code and then breaking them up for SQL query

I am trying to devlop a regex for finding camel case strings in several code files I am working with so I can break them up into separate words for use in a SQL query. I have strings of the form...

EmailAddress
FirstName
MyNameIs

And I want them like this...

Email Address
First Name
My Name Is

An example SQL query which I currently have is...

select FirstName, MyNameIs from MyTables

I need the queries in the form...

select FirstName as 'First Name', MyNameIs as 'My Name Is' from MyTables

Any time a new capital letter appears that should be a new grouping which I can pick out of the matched string. I currently have the following regex...

([A-Z][a-z]+)+

Which does match the cases I have shown above but when I want to perform a replace I need to define groups. Currently I have tried...

((开发者_运维问答[A-Z])([a-z]+))+

Which sort of works. It will pick out "Address" as the first grouping from "EmailAddress" as opposed to "Email" which is what I was expecting. No doubt there is something I'm misunderstanding here so any help is greatly appreciated.


You can find words that use PascalCase or camelCase using the regex

\w+(?<=[a-z])(?=[A-Z])\w+

You can then search the found words for

(?<=[a-z])(?=[A-Z])

replacing all these occurences with a space.

In Python (just to show what I mean):

import re

def prettify(string):
    find_re = "\w+(?<=[a-z])(?=[A-Z])\w+"
    split_re = "(?<=[a-z])(?=[A-Z])"
    words = re.findall(find_re, string)
    for word in words:
        split_word = re.sub(split_re, " ", word)  
        # or use an underscore instead of a space
        string = string.replace(word, word + " as '" + split_word + "'")
    return string

print prettify('select FirstName, MyNameIs from MyTables')

Output:

select FirstName as 'First Name', MyNameIs as 'My Name Is' from MyTables as 'My Tables'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜