shamuraq Posted June 26 Share Posted June 26 I have this list as an array: 1. [Question - Geography Chapter2] How would you describe humans' relationship with the physical environment? (Page 42) 2. [Question - Geography Chapter4] What is a natural resource? (Page 67) 3. [Question - Geography Chapter3] What are two or three resources which you cannot do without? What are the reasons for your choices? (Page 52) ... 129. [Question - Geography Chapter6] What is the human-centered view on the use of natural resources? (Page 339) 130. [Question - Geography Chapter7] What are the benefits of using products with a smaller environmental footprint? (Page 342) I would like to remove: - The square brackets and everything in it. I am hoping that the questions would start without the eg; [Question - Geography Chapter7] etc. - The round brackets and everything in it. I am trying to delete the references to the page number. Is there a simple an clean way to do it? Any pointer is truly appreciated. Thanx in advance. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted June 26 Solution Share Posted June 26 One way is to find the positions of the "]" and and the "(" and grab the text between those points. $qa = [ "[Question - Geography Chapter2] How would you describe humans' relationship with the physical environment? (Page 42)", "[Question - Geography Chapter4] What is a natural resource? (Page 67)", "[Question - Geography Chapter3] What are two or three resources which you cannot do without? What are the reasons for your choices? (Page 52)" ]; foreach ($qa as $k => &$q) { $p1 = strpos($q, ']'); $p2 = strpos($q, '('); $q = trim(substr($q, $p1+1, $p2-$p1-1)); } Quote Link to comment Share on other sites More sharing options...
requinix Posted June 26 Share Posted June 26 I'd ask exactly how confident you are that the strings are definitely going to look the way you think. If you're 100% sure they will definitely be "[Stuff] Text (Page 123)" then great, but if not then... Quote Link to comment Share on other sites More sharing options...
Danishhafeez Posted June 27 Share Posted June 27 Certainly! You can achieve this by using Python and regular expressions (regex) to clean up your list of questions. Here’s a step-by-step guide on how to remove the square brackets and their contents (including "Question - ...") and the round brackets with their contents (including the page numbers): import re # Example array of questions questions = [ "[Question - Geography Chapter2] How would you describe humans' relationship with the physical environment? (Page 42)", "[Question - Geography Chapter4] What is a natural resource? (Page 67)", "[Question - Geography Chapter3] What are two or three resources which you cannot do without? What are the reasons for your choices? (Page 52)", "[Question - Geography Chapter6] What is the human-centered view on the use of natural resources? (Page 339)", "[Question - Geography Chapter7] What are the benefits of using products with a smaller environmental footprint? (Page 342)" ] # Regex pattern to remove text within square brackets and round brackets pattern = r"\[.*?\]|\(.*?\)" # Function to clean each question def clean_question(question): return re.sub(pattern, "", question).strip() # Clean all questions cleaned_questions = [clean_question(q) for q in questions] # Print cleaned questions for idx, question in enumerate(cleaned_questions, 1): print(f"{idx}. {question}") Regex Pattern: \[.*?\] matches everything inside square brackets, including the brackets themselves. \(.*?\) matches everything inside round brackets, including the brackets themselves. .*? is a non-greedy match to ensure we match the shortest sequence possible within the brackets. Function clean_question: Uses re.sub() to substitute the matched patterns (square and round brackets and their contents) with an empty string "". Cleaning Process: cleaned_questions is a list comprehension that iterates over each question in the questions list, applies the clean_question function, and stores the cleaned question in cleaned_questions. 1. How would you describe humans' relationship with the physical environment? 2. What is a natural resource? 3. What are two or three resources which you cannot do without? What are the reasons for your choices? 4. What is the human-centered view on the use of natural resources? 5. What are the benefits of using products with a smaller environmental footprint? Best Rgeard danish Hafeez | QA Assistant ICTInnovations Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.