Split String Using Delimiter in Python Regular Expression

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

364 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

405 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

530 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

Difference Between throw and throws Keywords in Java

Sharon Christine
Updated on 20-Feb-2020 06:39:20

455 Views

The throw keyword is used to raise an exception explicitly.Examplepublic class Test {    public static void main(String[] args) {       throw new NullPointerException();    } }Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)The throws keywords in Java used to postpone the handling of a checked exception.public class Test {    public static void main(String[] args)throws NullPointerException {       throw new NullPointerException();    } }

Define a Class Inside a Java Interface

V Jyothi
Updated on 20-Feb-2020 05:46:54

7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library {    void issueBook(Book b);    void retrieveBook(Book b);    public class Book {       int bookId;       String bookName;       int issueDate;       int returnDate;    } } public class Sample implements Library {    public void issueBook(Book b) {       System.out.println("Book Issued");    }    public void retrieveBook(Book b) { ... Read More

Write Python Regular Expression to Match Multiple Words Anywhere

Rajendra Dharmkar
Updated on 20-Feb-2020 05:31:13

3K+ Views

The following code using Python regex matches the given multiple words in the given stringExampleimport re s = "These are roses and lilies and orchids, but not marigolds or .." r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X) print r.findall(s)OutputThis gives the output['roses', 'orchids', 'marigolds']

Eclipse Keyboard Shortcut for public static void main in Java

Ramu Prasad
Updated on 20-Feb-2020 05:29:13

9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Put jQuery Code in an External JS File

Ali
Ali
Updated on 20-Feb-2020 05:23:20

7K+ Views

Create an external JavaScript file and add the jQuery code in it.ExampleLet’s say the name of the external file is demo.js. To add it in the HTML page, include it like the following −                               Hello     Above we added jQuery using Google CDN and the external file was included after that.Add jQuery code in demo.js, since you wanted to place jQuery code −$(document).ready(function(){    alert(“amit”) });

Advertisements