the string
[playlist]numberofentries=2File1=http://66.162.107.142/cpr1_K128OV.oggTitle1=KCFR NewsLength1=-1File2=http://66.162.107.141:8000/cpr1_K128OV.oggTitle2=KCFR News BackupLength2=-1Version=2
i wanna cut all of the links in this file, how to?
The following class
package regexpso;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("(http:.*?.ogg)");
Matcher m = p.matcher("[playlist]numberofentries=2File1=http://66.162.107.142/cpr1_K128OV.oggTitle1=KCFR NewsLength1=-1File2=http://66.162.107.141:8000/cpr1_K128OV.oggTitle2=KCFR News BackupLength2=-1Version=2");
while (m.find()) {
System.out.println(m.group());
}
}
}
prints
http://66.162.107.142/cpr1_K128OV.ogg
http://66.162.107.141:8000/cpr1_K128OV.ogg
as result.
Use a regular expression to find and replace the URLs. Be aware this sort of thing is fraught with peril. Post an example of what you want the end result to look like for a better answer. Are all the URLs IP addresses?
Related
In java this is possible
public static void main(String[] args) {
Matcher m = Pattern .compile("^(.*?[.].*?[.].*?[.].*?)[.].*")
.matcher(
"com.SEM.Google.Generico.space.test");
if (m.matches()) {
System.out.println(m.group(1));
}
}
This would give me as result: com.SEM.Google.Generico
If I have a string in mongodb
"dv" : "com.SEM.Google.Generico.space.test"
can I use the mongo aggregation framework somehow to get com.SEM.Google.Generico as result?
It should be as generic as possible. So not something like
$project: {
pathString: {
$substr: ["$path.dv", 0, 23]
}
}
Is this possible at all?
Thanks.
No, there is no way to do this.
This feature has been requested two years ago, but it hasn't been implemented yet ( see the open jira issue: https://jira.mongodb.org/browse/SERVER-11947 ).
If you don't want to use $substr I guess that you should apply the regex on the query results...
var pattern = "Your Regex Pattern"
db.yourCollectionName.find( { "dv": { $regex: pattern} } )
should produce the desired results
first I want to say that I'm beginer and that this is my first Java program. I'd like to make a program that will read text file, find specific line, and save it to my string variable.
So I want to find line that starts with "Dealt to ", and then in that line, copy everything after that till this char '[' and put it in my string variable.
So let's say that I have this line in my text file:
Dealt to My NickName [text]
I want to have a program that will find text "My Nickname" and put it in my string variable.
I'm trying to work with classes and trying to use setters and getters just to practice, please let me know how my code looks like and how I can improve it and make it work.
this is Main.java:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
HandHistory hh1 = new HandHistory();
String hero1 = null;
hero1 = hh1.getHero();
System.out.println(hero1);
}
}
My HandHistory.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class HandHistory {
private String hero;
public HandHistory(){}
public String getHero() throws IOException {
FileReader in = new FileReader("G:/Java/workspace/HandHistory/src/File.txt");
BufferedReader br = new BufferedReader(in);
String line;
while ((line = br.readLine()) != null) {
if (line.contains("Dealt to ")){
hero = line.substring(9,(line.indexOf("["))-1);
}
}
return hero;
}
public void setHero(String hero){
this.hero = hero;
}
}
It's a good start, good way to read a file line by line. The one problem worth fixing is closing the FileReader resource by using a try-finally block, or since Java 7 the new try-with-resources block:
try (FileReader in = new FileReader("G:/Java/workspace/HandHistory/src/File.txt")) {
...
}
Other tips and comments I can think of:
You don't have to have a setter in your class if you don't actually need it
Your code doesn't work will if there are lines contain the string "Dealt to" but don't start with that string. E.g. "Foobar Dealt to My NickName [text]" will still be matched but will return a wrong value
If you really only want to match lines that start with "Dealt to" then use String.startsWith() instead of String.contains()
You should handle the case when there's no "[" in the string, otherwise your code crashes with a hard to understand error
Regular expressions are useful if they remove complexity from your code. In your case the problem can be solved by using startsWith and indexOf relatively easily, so I'd not use RegExps in this case
It's not obvious what HandHistory.getHero() does without looking at the actual code. It's always very helpful even for yourself to assign names to things that express what the class or method is actually doing.
It can be said that getHero() method does too many things and so does the class HandHistory, but that's maybe something to consider when using the code for something bigger than a learning hello-world example.
My advise would be to use a Regex. You can try with
(?<=beginningstringname)(.*\n?)(?=endstringname)
So, for your problem this would be
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "Dealt to My NickName [text]";
String pattern = "(?<=Dealt to )(.*\n?)(?=[)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
//m now haves what you desire. You can loop it if you want.
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}
Try this tutorial for using regular expressions in Java http://www.tutorialspoint.com/java/java_regular_expressions.htm
I have problem in creating a regex which has to accept only those URL's which have # in it and neglect others. I'm using urlValidator and it has a RegEx which allows URL's without # in them. can someone help me with buliding a RegEx.
I have problems with the RegEx. just help me in building a RegEx which would validate only those URL's which have # in it
This is my code
import org.apache.commons.validator.routines.RegexValidator;
import org.apache.commons.validator.routines.UrlValidator;
public class TestClass {
public static void main(String[] args) {
String url = "https://www.testforregex.com/test";
String[] schemes = {"#([^\\?|\\/|$]*)"};
RegexValidator regex= new RegexValidator(schemes);
UrlValidator urlValidator = new UrlValidator(regex,0);
System.out.println(urlValidator.isValid(url));
}
}
For checking if a string contains a certain character, using regex is a little overkill. You could just use String.contains(). So in your case:
boolean isValid = url.contains("#");
I have input stream with the following data:
---------------------------------------------
manil#manil-ubvm:~$ db2level
DB21085I Instance "manil" uses "64" bits and DB2 code release "SQL10010" with
level identifier "0201010E".
Informational tokens are "DB2 v10.1.0.0", "s120403", "LINUXAMD64101", and Fix
Pack "0".
Product is installed at "/home/manil/sqllib".
---------------------------------------------
From above i need v10.1.0.0 to be stored in a string variable.
How to do that using java regular expression?
Use something like this to capture the version pattern :
import java.util.regex.*;
public class RTest {
public static void main(String [] args) {
String raw_data = "asdkgjasdbf984 sdkjfashfiu 4qwsadkfjnv w98sa-asdf08gywbfsd v1231.123.12.11.1 fkjsdfn9823isd";
Pattern version_find = Pattern.compile("v[\\d+\\.?]+");
Pattern directory_find = Pattern.compile("[\\/[^\\/]+]+");
Matcher version_finder = version_find.matcher(raw_data);
while(version_finder.find()) {
System.out.println(version_finder.group());
}
}
}
Output is :
v1231.123.12.11.1
/isd/asdasd2903 ajshdaq09r34/adsj 38/
You really need to understand regexes deeply if you are a programmer. They are one of the essentials. They are hard at first, but once you 'crack them' you don't forget it. Like riding a bike.
This will suit your needs:
String version = yourLine.replaceAll(".*(v\\d+([.]\\d+){3}).*", "$1")
You dont need regularExpression here
just use
String .contain() method and String substring()
I'm looking to clean everything but the Class name off of a fully qualified Class name. So, I may have something like.....
"class gqlMain.Node"
... and I'd like to end up with....
"Node"
...I'm pretty sure my pattern...
"*.[\\.][^\\.]*"
..is correct, and when if simply run it as above and test with...
myMatcherObject.matches()
...it always returns true, but when I attempt to add groupings, like...
"(.*[\\.])([^\\.]*)"
...I always get a no match found error. Not sure what's going on.
ADDED:
Thanks for the quick responses, guys. Yeah, I really don't get this. My exact code is....
public String toString() {
Pattern packagePatt = Pattern.compile("(.*[\\.])([^\\.]*)");
//
System.out.println(this.compClass.getName().toString());
Matcher packageMatch = packagePatt.matcher(this.compClass.getName().toString());
//
System.out.println(packageMatch.group(2));
return packageMatch.group(2);
}
The first print statement produces a String like "gqlMain.Node", for example (I know the toString() is redundant, I added it out of exasperation). The second print statement produces an error, as would the return statement. With a debugger I can see that the groups List for the Matcher object remains empty at every index. But if I insert a...
if (packageMatcher.matches()) {
// print true
}
... I always get 'true'. This really makes no sense.
The following program reported "true":
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class so {
/**
* #param args
*/
public static void main(String[] args) {
Pattern p = Pattern.compile("(.*[\\.])([^\\.]*)");
Matcher m = p.matcher("class gqlMain.Node");
System.out.println(m.matches());
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
The full output is:
true
class gqlMain.Node
class gqlMain.
Node
I wouldn't recommend to scan for the identifiers in that way (but I believe you wanted not to over-engineer), and you probably will like the following solution that is more strict for scanning the identifiers in general (however, speaking frankly, I don't believe I'm scanning for an identifier in the most correct way too). Additionally, it can scan for several fully/partially qualified identifiers within a single string, but it completely ignores non-qualified one (e.g. class is ambiguous).
package stackoverflow;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.out;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;
public final class Q11554180 {
private Q11554180() {
}
//
// (3) The same as item (1) however we're ------------------------------------------------+
// capturing the group to get the class |
// name only |
// (2) At least one package name is required ---------------------------------+ |
// (1) We're searching valid package names only -----------------+ | |
// and we do not need to capture it ?: | | |
// +----------------+--------------+|+-------------+-------------+
// | ||| |
private static final Pattern pattern = compile("(?:[\\p{Alpha}_][\\p{Alnum}_]*\\.)+([\\p{Alpha}_][\\p{Alnum}_]*)", CASE_INSENSITIVE);
private static void find(CharSequence s) {
final Matcher matcher = pattern.matcher(s);
while ( matcher.find() ) {
out.println(matcher.group(1));
}
}
public static void main(String[] args) {
find("class gqlMain.Node; class gqlMain.p1.NodeA");
find("class gqlMain.p1.p11.NodeB");
find("class gqlMain.p1.p11.p111.NodeC");
find(Q11554180.class.getCanonicalName());
}
}
The code above will produce the following output:
Node
NodeA
NodeB
NodeC
Q11554180