--
--
Sub, Unsub, Read-on-the-web, tune your personal settings for this Regex forum:
http://groups.google.com/group/regex?hl=en
---
You received this message because you are subscribed to the Google Groups "Regex" group.
To unsubscribe from this group and stop receiving emails from it, send an email to regex+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Dear Kris,
I’ve put a little time in it for you. I am not a JAVA expert, but have a little experience in it; I did a course once J
I’ve used google to see how matches and patterns work:
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
It would seem you would like a class that takes the string, and a parameter (for instance DEV) and then returns the second argument. Now first of all you have got some things wrong here: Matches will not give you the pattern, but will test the string. You can get the match with the group() method, with an optional integer depicting the occurance, so if
".*("+p1+")\\s+(\\S+).*" is the pattern, and 2 is the occurance of the group you want to return (\\S+). That being said lookbehind will not be needed, since we can just capture the group as depicted.
One more caveat: You are matching multiline text.
Google tells us:
http://stackoverflow.com/questions/3651725/match-multiline-text-using-regular-expression
So set DOTALL property on the pattern when compiling it, to make the . match linefeeds.
So then I would get to the following code.
[CODE]
//Title of this code
//'main' method must be in a class 'Rextester'.
// get the matcher and pattern class
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// apparently these are needed.
import java.util.*;
import java.lang.*;
// class returnval holds one method 'Getit'. It expexts a string and an argument
class returnval
{
public String getit(String txtin, String p1)
{
// create pattern using parameter p1. Use brackets to reference what you want to match (\\S)
// set DOTALL so . wil match linefeed
Pattern pattern = Pattern.compile(".*"+p1+"\\s+(\\S+).*", Pattern.DOTALL);
// so pattern states 'anything followed by p1 followed by zero or more whitespaces followed by zero or more non-whitespaces
// construct matcher object from the pattern. Use text as input.
Matcher matcher = pattern.matcher(txtin);
if (matcher.matches()){
// if match is found, return the first captured group
return matcher.group(1);
}
else
{
return "foobar";
}
}
}
// main class
class Rextester
{
public static void main(String args[])
{
// filled statically now, should be replaced with file input
String txtfile="DEV dfathsnbfag+fsfdbvdsafwrehtgdbfagre/fgsdadewtreytg\n" +
"CERT dfasdfadf23453yhrew456ehgfnbdfge65ieyjtwujsjyi464\n"+
"PERF sh4u364746uerthdmhfyo67r586rutsjdko7+tjdmnsh/ea\n"+
"PROD sdnbry46857ukfghfdjrtue65i+reywurjsgnh/aq35uwetsjn";
// filled statically now, can be any string. Maybe you should test for valid strings first.
String p1="PROD";
// create object of the returnval class
returnval returnval = new returnval();
// call the getit method using parameter and input
System.out.println(returnval.getit(txtfile, p1));
}
}
[/CODE]
Little test on
P1=”PROD”
Returns sdnbry46857ukfghfdjrtue65i+reywurjsgnh/aq35uwetsjn
P1=”DEV”
Returns
dfathsnbfag+fsfdbvdsafwrehtgdbfagre/fgsdadewtreytg
That seams to work.
Op maandag 29 juli 2013 17:59:26 UTC+2 schreef Kris Ring het volgende: