1

I have a export file that contains data of fixed widths:

Name (6) Gender (6) Phone Number (12) - Includes a space

Data.txt

DanielMale  (07654) 521254
Lisa  Female(16545) 654456
Sarah Female(54656) 4896546

I need to extract the name and gender data including any spaces if the data doesn't fit the data width.

The brackets for the phone number need to be ignored. (How do you ignore items in regular expressions?

I currently have the following regular expression, that pulls out the people's names. I thought I could simply add the bit on the white to make it pull out the Genders, but this doesn't work. Where am I going wrong?

/(?<name>.{6}+)        (?<gender>.{6}+)/

I need the data to look like this at the end.

^ = space

Daniel
Male^^
07654 521254

Lisa^^
Female
16545 654456

Sarah^
Female
54656 4896546

1 Answer 1

1

This should catch all four fields:

/^(?<name>.{6})(?<gender>.{6})\((?<prefix>[^\)]+)\)\ (?<number>.+)/
  • The first ^ means match from the start.
  • {6}: match 6 times the pattern (the plus sign is redundant here)
  • \( and \): match brackets (without escaping they would mean boundaries of a subpattern)
  • [^\)]+ means "everything before the first closing bracket
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.