3

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.

Community
  • 1
  • 1
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265

2 Answers2

1

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

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
1

You can't work with regex101.com as Its accessible engines don't support \U and \L replacement tokens. However Notepad++ is okay.

At first, use this regex to match those spaces/punctuation and their following character:

[^[:alnum:]\n\r](\w)?

replace it with

\U$1

enter image description here Second, match the recent whole line:

^(?i)([a-z\d]+)$

and replace with:

public string $1 { get; set;}

enter image description here

revo
  • 47,783
  • 14
  • 74
  • 117
  • Hi, didn't handle cases with numbers. `Executor Title Address Line 1 Address Line 2 Address Line 3 Address Line 4 Address Line 5 Address Line 6 Post Code Home Phone Number Mobile Number Email Notes to solicitor Are any of the children under 18 years old? Do you have executor(s)?` – Matas Vaitkevicius Oct 24 '14 at 08:29
  • After update `[^[:alnum:]\n\r]\d*(\w)?` removes numbers that produces 6 x AddressLine without number at the end (Address Line 1 and Address Line 2 are two distinct properties) – Matas Vaitkevicius Oct 24 '14 at 08:34
  • @LIUFA I rolled back first regex, modified second one. Check update. – revo Oct 24 '14 at 08:38
  • Works perfectly, thank you very much. I have accepted other answer as @Lucas was first but I have gave you same amount of rep by + your other answers. – Matas Vaitkevicius Oct 24 '14 at 08:41