Skip to main content

Regex - Dealing with Groups, C#


Just say you have a string like this one....

Job Posting: 10 Jul 2020 - Closing Date: 26 Jul 2020 Land Administration Legal and Justice Regulatory and Compliance Central & North Coasts Job Title: Senior Analyst Native Title Job Grade / Classification: Clerk Grade 9/10 Employment Type: Temporary role for 24 months Location: Negotia... 


and you want to insert a new line after the date, 26 Jul 2020. 

Details = "Job Posting: 10 Jul 2020 - Closing Date: 26 Jul 2020 Land Administration Legal and Justice Regulatory and Compliance Central & North Coasts Job Title: Senior Analyst Native Title Job Grade / Classification: Clerk Grade 9/10 Employment Type: Temporary role for 24 months Location: Negotia... "; 


  Regex r1 = new Regex(@"(.*Closing Date: \d\d [A-Za-z]* \d\d\d\d)(.*)");


Match match = r1.Match(Details);

if (match.Success && match.Groups.Count == 3 )

{

j.JobDates = match.Groups[1].Value.Trim();

j.Details = match.Groups[2].Value.Trim();

}else{

j.Details = Details ; 

}


Assuming you've created a j object with JobDates and Details properties, you'll end up with something like 


Note, the Groups array element at position 0 is the full text, that's why it starts at element 1. 


 JobDates Job Posting: 10 Jul 2020 - Closing Date: 26 Jul 2020
 Details Land Administration Legal and Justice Regulatory and Compliance Central & North Coasts - Newcastle & Hunter Valley / Newcastle, Hunter & Region Central & North Coasts - Newcastle & Hunter Valley / Port Macquarie & Mid-North CoastRegional NSW / Goulburn & Southern Tablelands Regional NSW / Bathurst & Central West NSW Sydney Region / Sydney City Job Title: Senior Analyst Native Title Job Grade / Classification: Clerk Grade 9/10 Employment Type: Temporary role for 24 months Location: Negotia...





Comments

Popular posts from this blog

Javascript Form Validation

It's real simple. All you need to do is call a javascript function in a html  " onsubmit " in the form tag as a javascript return, like this       <form method="post" action="dosomething.php" onsubmit="return validateForm();"> If the code completes ok, the form is then sent to the page listed in "action" http://www.w3schools.com/js/js_form_validation.asp If the function specified in the onsubmit returns false, then the form is not sent to the page mentioned in action.