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
python Write a Loop class. The contents of a loop object are represented internally as a...
python Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables: loop - a python list containing the elements of the loop list head - which stores the index of the first element of the loop list current - which stores the index of the last element of the loop list size - which stores how many actual elements are in the loop max - which...
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this...
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this try to use a level one skill python, like as if you just learned this don't use anything advanced if you can please. If not then do it as you can. Assume you have a file on your disk named floatnumbers.txt containing float numbers. Write a Python code that reads all the numbers in the file and display their average. Your code must handle...
Create a python script that reads in the contents of CityPop.csv and stores the data in...
Create a python script that reads in the contents of CityPop.csv and stores the data in a container. Task 2 will expect that your program can find cities by name within the container, so think about that as you set up your data container. Briefly describe how you store the data in comments. *It won't let me post the CSV file
Write this code in Python only. Draw a 12" ruler on the screen. A ruler is...
Write this code in Python only. Draw a 12" ruler on the screen. A ruler is basically a rectangular outline with tick marks extending from the top edge. The tick marks should be drawn at each quarter-inch mark. Below the tick marks, your ruler should show large integers at each full-inch position.
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...
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 ---
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,...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT