Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to Remove Special characters and Spaces and Then Camel Case It

Tags:

regex

I am trying to generate C# Properties by using regex out of question names. I have more than 100 and task is likely to be repeated so it's worth putting effort into it.

Strings to be converted:

Do you own your property?
How is it owned?
Relationship to other registered owner
Estimated value of Estate (joint)
Address Line 1
Are any of the children under 18 years old?
Are you interested in safeguarding your assets for your children/beneficiaries?

Expected outcome:

public string DoYouOwnYourProperty { get; set;}
public string HowIsItOwned { get; set;}
public string RelationshipToOtherRegisteredOwner { get; set;}
public string EstimatedValueOfEstateJoint { get; set;}
public string AddressLine1 { get; set;}
public string AreAnyOfTheChildrenUnder18YearsOld { get; set;}
public string AreYouInterestedInSafeguardingYourAssetsForYourChildrenBeneficiaries { get; set;}

I was snooping around and found flavours of regex questions where they would remove special chars or would UpperCase it in code but not do both with regex only.

I started out with this

([a-zA-z])( [a-zA-z])([a-zA-z])

Replace:

\1\U2\3

However group 2 does not get upper-cased, and I am not sure how to append public string and { get; set;} for everything and not per group.

like image 284
Matas Vaitkevicius Avatar asked Sep 16 '25 22:09

Matas Vaitkevicius


1 Answers

In Notepad++:

Step 1: Remove punctuation

Replace the following pattern:

[^\w\s]

With an empty string.

Step 2: Remove spaces and make letters uppercase

Replace the following pattern:

[^\S\r\n]+(\d*\p{L}|\d+)

With this:

\U$1

Step 3: Add the property syntax

Replace the following pattern:

^(\S+)$

With this:

public string $1 { get; set; }

For reference, here's what Notepad++ uses for regex replacements: Boost-Extended Format String Syntax

like image 133
Lucas Trzesniewski Avatar answered Sep 19 '25 23:09

Lucas Trzesniewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!