I have a very complicated output from a function, which i need to use specific word from it.
For example, my output is:
oracle 11257 1 0 14:01 ? 00:00:00 ora_pmon_sas
I need to get just "sas" word, which is next to "ora_pmon_"
Another example:
oracle 6187 1 0 13:41 ? 00:00:00 ora_pmon_db2
I need to get "db2". So what should be my expression?
JAVA code:
insArray=line.split("what will be between these quotes?");
You could just do
String sub = s.substring(s.indexOf("ora_pmon_") + 9);
How about this one?
string = string.replaceAll(".*?ora_pmon_", "");
If you want multiple words in place of ora, then it will be
string = string.replaceAll(".*?(ora|kf|asm)_pmon_", "");
You can simply use String#substring(int i) combined with String#lastIndexOf(char ch)
For example:
String result = input.substring(input.lastIndexOf('_') + 1)
Related
So I have this regex that does what I need it to for the most part, or so I thought..
String[] part = str.split("\\b");
This will take a string such as " int(x,y) " and separate it into 6 new strings.
1. int
2. (
3. x
4. ,
5. y
6. )
BUT I just realized that my regex is not doing this with underscores? For example the string " ret_urn" is not split at all. Is it possible to add an "AND" to my regex to include underscores?
You can use something like below to get the result you expect with an or,
String str = "int(x,y)_ret_urn";
str.split("\\b|((?<=_)|(?=_))");
I would like to do the following thing:
I have a String in Java, for instance
"4231354"
My aim is to show a version number in this way
XXX-X-X.XX
which means than I need to use a function to show
"423-1-3.54"
Thanks.
You can try this too:
StringBuilder str = new StringBuilder("4235167");
str.insert(3, '-');
str.insert(5, '-');
str.insert(7, '.');
System.out.println(str);
You can do it with replaceAll, like this:
String ver = "4231354";
String fmt = ver.replaceAll("(\\d{3})(\\d)(\\d)(\\d{2})", "$1-$2-$3.$4");
Regular expression defined by the first parameter has four groups - a group of three characters, two groups of single characters, and a group of two characters. The second parameter uses these groups, which are numbered $1 through $4, to produce the formatting that you are looking to achieve.
Demo.
I like the old way
As you mentioned length is fixed so you can use substring
String st="4231354";
String newString=st.substring(0,3)+"-"+st.substring(3,4)+"-"+st.substring(4,5)+"."+st.substring(5,st.length());
System.out.println(newString);
DEMO
If you want to get
"423-1-3.54"
All you have to do is:
String originalNumber = "4231354";
String versionNumber = new String(originalNumber.substring(0, 3) + "-" + originalNumber.substring(3, 4) + "-" + originalNumber.substring(4, 5) + "." + originalNumber.substring(5));
Or use String.format()
public Format(String version) {
String v = String.format("%s-%s-%s.%s",
version.substring(0,3),
version.substring(3,4),
version.substring(4,5),
version.substring(5,7));
System.out.println(v);
}
Hi I have a question about how to get only part of given string :
String = "1. Name:Tom\tNumber:123";
in this case I would like to get only part with name ( "Tom" )
Is there is any solution to do it?
Thank's for help
This is a simple way assuming the format won't change.
int start = yourString.indexOf(':') + 1; //get character after this
int end = yourString.indexOf('\t');
String result = yourString.substring(start,end);
You can also write your own regex to match this.
If that format is how it always shows up this would work:
String str = "1. Name:Tom\tNumber:123";
String tom = str.substring(str.indexOf(':')+1, str.indexOf('\t'));
Using the first occurrence of : and the \t allows you to parse out the name. In the case of your :, you want to begin your string 1 character ahead.
Result:
Tom
What you need, you will easyly find at the String documentation.
I have a String like this:
http://www.fam.com/FAM#Bruno12/06/2011
How can I cut http://www.fam.com/FAM# and 12/06/2011 in order to get only Bruno.
The format is always:
http://www.fam.com/FAM#NAMEDATE
Is there a simple way to do this? Can you just explain me how?
Simply do this:
myString = original.substring(23, original.length() - 10);
23 is for http://www.fam.com/FAM#
original.length() - 10 is for 12/06/2011
Use :
String str = "http://www.fam.com/FAM#Bruno12/06/2011";
String[] arr = str.split("#|[\\d+/]"); // last index of arr is Bruno
If the string always starts with http://www.fam.com/FAM# then it's simple: that's 23 characters, so take the substring from position 23 (note that indices are zero-based).
String input = "http://www.fam.com/FAM#Bruno12/06/2011";
String result = input.substring(23);
If you want everything after the first # in the string, then search for # and take everything that comes after it:
int index = input.indexOf('#');
String result = input.substring(index + 1);
(error checking omitted for simplicity).
To remove the date, remove the last 10 characters.
See the API documentation of class String for useful methods.
use regex #(.*?)\\d\\d/ to capture it.
You should use standard URL parsing code, as at Could you share a link to an URL parsing implementation?
I expect the URL parser can cope with the fact that your Ref (i.e. "Bruno12/06/2011") contains slashes.
URL url = new URL(inputString);
String nameDate = url.getRef();
expresses what you want to do in the simplest and clearest form.
I need to split a string base on delimiter - and .. Below are my desired output.
AA.BB-CC-DD.zip ->
AA
BB
CC
DD
zip
but my following code does not work.
private void getId(String pdfName){
String[]tokens = pdfName.split("-\\.");
}
I think you need to include the regex OR operator:
String[]tokens = pdfName.split("-|\\.");
What you have will match:
[DASH followed by DOT together] -.
not
[DASH or DOT any of them] - or .
Try this regex "[-.]+". The + after treats consecutive delimiter chars as one. Remove plus if you do not want this.
You can use the regex "\W".This matches any non-word character.The required line would be:
String[] tokens=pdfName.split("\\W");
The string you give split is the string form of a regular expression, so:
private void getId(String pdfName){
String[]tokens = pdfName.split("[\\-.]");
}
That means to split on any character in the [] (we have to escape - with a backslash because it's special inside []; and of course we have to escape the backslash because this is a string). (Conversely, . is normally special but isn't special inside [].)
Using Guava you could do this:
Iterable<String> tokens = Splitter.on(CharMatcher.anyOf("-.")).split(pdfName);
For two char sequence as delimeters "AND" and "OR" this should be worked. Don't forget to trim while using.
String text ="ISTANBUL AND NEW YORK AND PARIS OR TOKYO AND MOSCOW";
String[] cities = text.split("AND|OR");
Result : cities = {"ISTANBUL ", " NEW YORK ", " PARIS ", " TOKYO ", " MOSCOW"}
pdfName.split("[.-]+");
[.-] -> any one of the . or - can be used as delimiter
+ sign signifies that if the aforementioned delimiters occur consecutively we should treat it as one.
I'd use Apache Commons:
import org.apache.commons.lang3.StringUtils;
private void getId(String pdfName){
String[] tokens = StringUtils.split(pdfName, "-.");
}
It'll split on any of the specified separators, as opposed to StringUtils.splitByWholeSeparator(str, separator) which uses the complete string as a separator
String[] token=s.split("[.-]");
It's better to use something like this:
s.split("[\\s\\-\\.\\'\\?\\,\\_\\#]+");
Have added a few other characters as sample. This is the safest way to use, because the way . and ' is treated.
Try this code:
var string = 'AA.BB-CC-DD.zip';
array = string.split(/[,.]/);
You may also specified regular expression as argument in split() method ..see below example....
private void getId(String pdfName){
String[]tokens = pdfName.split("-|\\.");
}
s.trim().split("[\\W]+")
should work.
you can try this way as split accepts varargs so we can pass multiple parameters as delimeters
String[]tokens = pdfName.split("-",".");
you can pass as many parameters that you want.
If you know the sting will always be in the same format, first split the string based on . and store the string at the first index in a variable. Then split the string in the second index based on - and store indexes 0, 1 and 2. Finally, split index 2 of the previous array based on . and you should have obtained all of the relevant fields.
Refer to the following snippet:
String[] tmp = pdfName.split(".");
String val1 = tmp[0];
tmp = tmp[1].split("-");
String val2 = tmp[0];
...