Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- Extract part of a string between two special characters

Tags:

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.

like image 712
bryan Avatar asked Feb 10 '11 20:02

bryan


People also ask

How do I extract text between two delimiters?

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.

How do you find the string between two characters?

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.

How to grab everything after special character _ and * in Java?

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?

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?

How to extract between two characters in 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:

How to extract the string between two delimiters in Java?

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 = "\ [ (.*?)\]";


2 Answers

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

like image 124
Mark Byers Avatar answered Oct 13 '22 00:10

Mark Byers


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]; 
like image 33
Goran Jovic Avatar answered Oct 12 '22 23:10

Goran Jovic