Question

In: Computer Science

Python regex How to write the regex in Python that only include the contents within a...

Python regex

How to write the regex in Python that only include the contents within a parentheses?

e.g. "uio" [ek3k 0die] 4229834

return "ek3k 0die"

Solutions

Expert Solution

How to write the regex in Python that only include the contents within a parentheses?

e.g. "uio" [ek3k 0die] 4229834

return "ek3k 0die"

code to get the content within the bracket

save the file with .py extension

or execute in python console:

Code

import re #regular expression module imported

a='"uio" [ek3k 0die] 4229834' # declare your string

o=re.search(r"\[((\w*\s*\w*)*)\]",a,re.X) # we will use search function to match the regular o.group(1) # to access search object by group method #expression

**********************************************************

regular expression explanation

it shoud contain open square bracket parentheses and followed by any charater and any number of times and ends with closed square bracket parentheses

* => it indicates 0 or more times occurences

/[ ( (\w* \s* \w*)* ) \]

first open bracket then any character any number of times(denoted by \w* ) any number of spaces (\s*) any number of characters (\w*) and whole * to repeat the pattern.

output:

DONT FORGET TO IMPORT RE


Related Solutions

Regarding Python. 1. How would you write a regex that matches the hour of the day...
Regarding Python. 1. How would you write a regex that matches the hour of the day from the extracted line shown below? (The highlighted portion) From [email protected] Sat Jan 5 09:14:16 2008 Question options: ˆFrom .+ [0-9] ˆX-.*: [0-9.]+ [0-9]: ˆFrom .* [0-9][0-9]: ˆFrom .* ('ˆX\S*: ([0-9.]+)', line) A _____________ sign at the end of the regular expression indicates that the string must end with the specified regex pattern. Question options: question (?) caret (^) dollar ($) 0 / 1...
Python Programming: Write a RegEx that validates social security numbers
Python Programming: Write a RegEx that validates social security numbers
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and...
PLEASE INCLUDE #FOR EACH LINE EXPLANATION Write a PYTHON PROGRAM that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session: Input an integer: abc Error: try again. Input an integer: 4a Error: try again. Input an integer: 2.5 Error: try again. Input an integer: 123 The integer is: 123 Hint: the string isdigit method will be useful to solve this...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
create a python function (only) that generates 10 random numbers within a specific range. Every time...
create a python function (only) that generates 10 random numbers within a specific range. Every time a number is generated, it is appended to a list (you need to initialize a list). Then the function counts how many times a number is repeated in the created list (you can choose any number to count). The function returns the list, the number you have chosen and its count. --define the function here ---
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
how to write a unit test for a write function in python? is there a better...
how to write a unit test for a write function in python? is there a better way than running main once and checking whether the new file path exists?
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter...
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
python code You are to write a program which produces a triangle as seen below. •Include...
python code You are to write a program which produces a triangle as seen below. •Include a function named goingUpwhich accepts a value and prints lines of stars beginning at 1 star and ending at the number of stars which was sent to it. For example, if the value sent to it is 4, the function would print this:********** •Include a function named goingDown which accepts a value and prints lines of stars beginning at 1 LESS THAN the value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT