Get the Number of Capture Groups in Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:57:19

2K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

Extract Anchor Tags from Webpage using Python Regular Expressions

Rajendra Dharmkar
Updated on 20-Feb-2020 07:56:17

210 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

Use Wildcard in Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:49:42

2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

Find All Adverbs and Their Positions in Text Using Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:40:37

454 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

Extract Numbers from Text Using Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:17:09

2K+ Views

If we want to extract all numbers/digits  individually from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d', s) print resultOutput['1', '2', '3', '4', '5', '6', '7']If we want to extract groups of numbers/digits from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d+', s) print resultOutput['12345', '67']

Split String Using Delimiter in Python Regular Expression

Rajendra Dharmkar
Updated on 20-Feb-2020 07:13:40

352 Views

The re.split() methodre.split(pattern, string, [maxsplit=0]):This methods helps to split string by the occurrences of given pattern.Exampleimport re result=re.split(r'a', 'Dynamics') print resultOutput['Dyn', 'mics']Above, we have split the string “Dynamics” by “a”. Method split() has another argument “maxsplit“. It has default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string. ExampleLet’s look at the example below −import result=re.split(r'a', 'Dynamics Kinematics') print resultOutput['Dyn', 'mics Kinem', 'tics']ExampleConsider the following codeimport re result=re.split(r'i', 'Dynamics Kinematics', maxsplit=1) print resultOutput['Dyn', 'mics Kinematics']Here, you can notice that we have fixed the ... Read More

Where Should jQuery Code Go: Header or Footer?

Ali
Ali
Updated on 20-Feb-2020 07:01:23

2K+ Views

It’s always a good practice to add jQuery code in footer i.e. just before the closing tag. If you have not done that, then use the defer attribute.Use defer attribute so the web browser knows to download your scripts after the HTML downloaded −The defer attribute is used to specify that the script execution occurs when the page loads. It is useful only for external scripts and is a boolean attribute.ExampleThe following code shows how to use the defer attribute −                 The external file added will load later, since we're using defer    

Capture File Not Found Exception in Java

karthikeya Boyini
Updated on 20-Feb-2020 06:58:29

385 Views

While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample {    public static void main(String args[]) throws Exception {       File file = new File("myFile");       FileInputStream fis = new FileInputStream(file);       System.out.println("Hello");    } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified)       at java.io.FileInputStream.open(Native Method)       at java.io.FileInputStream.open(Unknown Source)       at java.io.FileInputStream.(Unknown Source) ... Read More

Capture Out of Array Index Out of Bounds Exception in Java

Swarali Sree
Updated on 20-Feb-2020 06:51:48

505 Views

When you try to access an element of an array at an index which is out of range, an ArrayIndexOutOfBoundsException exception is raised.ExampleLive Demopublic class ArrayIndexOutOfBounds {    public static void main(String args[]) {       try {          int[] a = new int[]{1,2,3,4,5};          int x = 6;          a[10] = x;       } catch(ArrayIndexOutOfBoundsException ex) {          System.out.println("Array size is restricted to 5 elements only");       }    } }OutputArray size is restricted to 5 elements only

Capture Divide by Zero Exception in Java

Swarali Sree
Updated on 20-Feb-2020 06:50:45

2K+ Views

When you divide a number by zero an Arithmetic Exception number is thrown.ExampleLive Demopublic class DividedByZero {    public static void main(String args[]) {       int a, b;       try {          a = 0;          b = 54/a;          System.out.println("hello");       } catch (ArithmeticException e) {          System.out.println("you cannot divide a number with zero");       }    } }Outputyou cannot divide a number with zero

Advertisements