Jump to content

Removing / editing certain keywords from array content


shamuraq
Go to solution Solved by Barand,

Recommended Posts

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.

Link to comment
Share on other sites

  • Solution

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));
 }

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.