Regex retrieve the value that matches
Hello,
I tested this snippet works:
Code:
String reg = "[0-9]*";
String in = "test0123test";
Boolean ismat = Pattern.games(reg, in);
but I try to retrieve the string "0123" a match that my expression "[0-9] *"
How? With this Regex how can I retrieve the value that matches. Thank you in advance
Re: Regex retrieve the value that matches
Hello,
It would be preferable to use the expression "[0-9]+"If you have lots of empty string in result
Code:
Matcher mth = Pattern.compiles(regex).mth(input);
while (mth.find()) {
System.out.System.out.println(mth.group());
}
Hope this will help you. If more problem then do post back.
Re: Regex retrieve the value that matches
Hello,
Thanks for your reply . I take this opportunity to ask you if you know a site where I can find regular expressions representing the dates like:
Quote:
08/11/2009
Wed, Jul 5, 02
02002.July.04 AD 12:08 PM
etc...
like what the class SimpleDateFormat, but unlike (of String to Date). The goal is to make a class that parses a text and return the Date. should I write the words one by one? Thank you
Re: Regex retrieve the value that matches
Hello,
But SimpleDateFormat can also return dates from String. Interested now in its parse method.
Code:
SimpleDateDormat sdf = new SimpleDateFormat("dd / MM / yyyy");
Date date = sdf.parse("20/04/1990");
Just try this, it is very easy with this way.
Re: Regex retrieve the value that matches
Hello,
I know this function but the problem with is that one must know the date format to be able to parse one! Now I want to parse a text (that of an email for example) and retrieve all appointments.
Quote:
eg I'll see you Friday, January 23, 2009.
I want the function to parse my post and finds it a date. I think this is bit semantic. Should I enter all the possible combination's? Any help on this is appreciated.
Re: Regex retrieve the value that matches
Hello,
Just have a look at the following example, this is the basic thing to do in your case
Code:
public class SampleRegex
{
public static void main(String[] params)
{
Pattern pat = Pattern.compile("(.*):(.*)");
Matcher mat = pat.mat(params[0]);
if(mat.matches())
{
System.out.print("Key:");
System.out.println(mat.group(1));
System.out.print("Value:");
System.out.println(mat.group(2));
}
else
System.out.print("No match");
}
}