I am new to java, i have a string
"rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23"
What I want is to get the Driving Test word. The word is dynamically changes so what I want to happen is get the word between the rdl_mod_name: and the \n.
Try the following.. It will work in your case..
String str = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
Pattern pattern = Pattern.compile("rdl_mod_name:(.*?)\n");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Also you can make use of regex,matcher,pattern to get your desired result..
The following links will also give you a fair idea:
Extract string between two strings in java
Java- Extract part of a string between two special characters
How to get a string between two characters?
I would look into java regular expressions (regex). The String matches method uses a regex to determine if there's a pattern in a string. For what you are doing, I would probably use 'matches(rdl_mod_.*\n)'. The '.*' is a wildcard for strings, so in this context it means anything between rdl_mod and \n. I'm not sure if the matches method can process forward slashes (they signify special text characters), so you might have to replace them with either a different character or remove them altogether.
Use java's substring() function with java indexof() function.
Try this code :
String s = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
String sArr[] = s.split("\n\n");
String[] sArr1 = sArr[1].split(":");
System.out.println("sArr1[1] : " + sArr1[1]);
The s.split("\n\n");will split the string on basis of \n\n.
The second split i.e. sArr[1].split(":"); will split the second element in array sArr on basis of : i.e split rdl_mod_name:Driving Test into rdl_mod_name and Driving Test.
sArr1[1] is your desired result.
Related
I want to match a string which occurs after a certain pattern but I am not able to come up with a regex to do that (I am using Java).
For example, let's say I have this string,
caa,abb,ksmf,fsksf,fkfs,admkf
and I want my regex to match only those commas which are prefixed by abb. How do I do that? Is it even possible using regexes?
If I use the regex abb, it matches the whole string abb, but I only want to match the comma after that.
I ask this because I wanted to use this regex in a split method which accepts a regex. If I pass abb, as the regex, it will consider the string abb, to be the delimiter and not the , which I want.
Any help would be greatly appreciated.
String test = "caa,abb,ksmf,fsksf,fkfs,admkf";
String regex = "(?<=abb),";
String[] split = test.split(regex);
for(String s : split){
System.out.println(s);
}
Output:
caa,abb
ksmf,fsksf,fkfs,admkf
See here for information:
https://www.regular-expressions.info/lookaround.html
I met a problem when I try to extract a segment from a string using Java. The original string is looks like test/data/20/0000893220-97-000850.txt, and I want to extract the segment which is behind the third /.
My regular expression is like
String m_str = "test/data/20/0000893220-97-000850.txt";
Pattern reg = Pattern.compile("[.*?].txt");
Matcher matcher = reg.matcher(m_str);
System.out.println(matcher.group(0));
The expected result is 0000893220-97-000850, but obviously, I failed. How can I correct this?
[^\/]+$
https://regex101.com/r/tS4nS2/2
This will extract the last segment in a string that contains after slashes. It would work great if you want that, as opposed to only the third section.
To find and extract the match, you don't need a match group (hence, no ()), however, you need to instruct the matcher to only look for the pattern, since .matches() will attempt to compare the entire string. Here is the relevant bit and here is a full example:
matcher.find(); //finds any occurrence of the pattern in the string
System.out.println(matcher.group()); //returns the entire occurence
Note the lack of index inside the call .group().
On a separate note, in Java, you don't necessarily need regex - extracting the last part can be done using plain Java
String matched = m_str.split('/')[2];
This would capture the third segment while
String[] matches = m_str.split('/');
String matched = matches[matches.length-1];
Would give you the last part.
How can I write a regex to see if a certain string is contained within two characters.
For example, I want to see which part of the string is contained within a quotation mark.
Java"_virtual_"machine
If I run my regex through this, I want to get _virtual_
How can I achieve this using regexes?
Supposing the text contains only one such pair of characters and there are no escaping tricks or so, you can use this regex
.*"([^"]*)".*
Try it online
I wouldn't do it with regex but with method which finds first and second occurrence of a character so it's more flexible to use in the
String s = "Java\"_virtual_\"machine";
String find = "\"";
int first = s.indexOf(find) + 1;
int second = s.indexOf(find, s.indexOf(find) + 1);
s.substring(first, second);
You can reuse this code on other characters and I bet it's faster than regex.
You can use this regex. I'm not sure if this is the best regex you can use though. It will capture every string between quotes.
.*?"([^"]*)"
Demo here
If you want to get the first string that matches the regex, use :
.*?"([^"]*)".*
I want to replace a part of a string with another string. For example, a string comes in, and it contains meat, but I want it to change that meat to vegetable. I know I can do this with the following statement.
str = str.replaceAll("meat", "vegetable");
However, I need to have the exact capitalization. I'm looking for a way to do this no matter what letters are upper or lowercase in the example meat.
My application is a filter for Minecraft. Either swears, or just customizing the menu. Like I'd like to replace every instance of the word minecraft, no matter the capitalization, to say Best game Ever!!.
I have modded it so that it can recognize and replace specific capitalizations, but that is very limiting.
I hope I have supplied enough info so that someone can help me with this.
You can make regex case-insensitive by adding (?i) flag at start
str = str.replaceAll("(?i)meat", "vegatable");
example
System.out.println("aBc def".replaceAll("(?i)abc","X")); // out: "X def"
The first argument to replaceAll is a regular expression, and you can embed a flag in the expression to make it case-insensitive:
str = str.replaceAll("(?i)meat", "vegatable");
Another way: Here the flag is not explicitly in the regex, but passed as a separate parameter.
String input = "abc MeAt def";
Pattern pattern = Pattern.compile("meat", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String output = matcher.replaceAll("vegetable");
System.out.println(output);
If I have a string "Sc_bookstore.PKG_book.SP_Harrypotter", for example.
How would I separate the schema, package and StoredProc? i.e, I want to get he following output:
Sc_bookstore
PKG_book
SP_Harrypotter
I am going to be using this regex on multiple StoredProc calls, and all of them follow the standard of "Sc_" "PKG_" "SP_"
Thanks in advance!
Use "\\." or "[.]" if a regular expression split delimiter is required (as with String.split).
A . in a Java regular expression means "match any character" (roughly, depending on options); the two forms above escape/prevent this meaning so it matches a literal period.
Any time you need to divide a string by a delimeter, you should consider using the String split method:
String s = "Sc_bookstore.PKG_book.SP_Harrypotter";
String parts[] = s.split("\\.");
parts[0]; // holds "Sc_bookstore"
parts[1]; // holds "PKG_book"
parts[2]; // holds "SP_Harrypotter"
If you really want/need Regex, something in the lines of:
^(Sc_\w+)\.(PKG_\w+)\.(SP_\w+)$
Would allow you to capture the groups values
For input Sc_bookstore.PKG_book.SP_Harrypotter:
Group 1: "Sc_bookstore"
Group 2: "PKG_book"
Group 3: "SP_Harrypotter"
But, in your case, just splitting the String by . would suit your needs very well
you may use split (with no regex)
String[] values;
values = str.split("\\.");
Assuming that a single string always contains exactly one such record, use this:
^Sc_([^.]+)\.PKG_([^.]+)\.SP_([^$]+)$
...note that this removes the Sc_, PKG_ and SP_ parts of the string and only returns "content".
For testing purposes, I created a simple JavaRegexTester application to test regular expressions.