I have been trying to figure out how to extract a portion of a string between two special characters ' and " I've been looking into regex, but frankly I cannot understand it.
Example in Java code:
String str="21*90'89\"";
I would like to pull out 89
In general I would just like to know how to extract part of a string between two specific characters please.
Also it would be nice to know how to extract part of the string from the beginning to a specific character like to get 21.
The easiest way to extract a substring between two delimiters is to use the text to column feature in Excel, especially if you have multiple delimiters. In this example, use =MID(A2, SEARCH(“-“,A2) + 1, SEARCH(“-“,A2,SEARCH(“-“,A2)+1) – SEARCH(“-“,A2) – 1) in cell B2 and drag it to the entire data range.
To get a substring between two characters:Get the index after the first occurrence of the character. Get the index of the last occurrence of the character. Use the String. slice() method to get a substring between the 2 characters.
In this tutorial we will go ver very simple example of grabbing everything after special character _ and *. Just open your Eclipse IDE, copy below code and try running as Java Application. String crunchifyStr = "Hey.. This is Crunchify.com"; // substring (): Returns a string that is a substring of this string.
How to extract certain substring from a string using Java? You can extract a substring from a String using the substring () method of the String class to this method you need to pass the start and end indexes of the required substring. How to extract a substring from inside a string in Python?
Here, the FIND (“/”,B5)+1 function returns the starting position of one text string that we want to extract between two characters and we will get the following output: The LEN (B5) function returns the number of characters in a text string like the following:
Create a regular expression to extract the string between two delimiters as regex = “\ [ (.*?)\]” and match the given string with the Regular Expression. Print the subsequence formed. Below is the implementation of the above approach: const regex pattern ("\ [ (.*?)\]"); import java.util.regex.*; String regex = "\ [ (.*?)\]";
Try this regular expression:
'(.*?)"
As a Java string literal you will have to write it as follows:
"'(.*?)\""
Here is a more complete example demonstrating how to use this regular expression with a Matcher
:
Pattern pattern = Pattern.compile("'(.*?)\""); Matcher matcher = pattern.matcher(str); if (matcher.find()) { System.out.println(matcher.group(1)); }
See it working online: ideone
If you'll always have a string like that (with 3 parts) then this is enough:
String str= "21*90'89\""; String between = str.split("\"|'")[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With