Select a specific part of a String in Java with PL SQL ARRAY
Hi
Many times when we develop systems we encounter problem like the following: We extract
fracmentos red characters or a string like this:
2 o. March - April
would be a good idea to do it with StringTokenizer but there are also other sensilla and elegant way to do it, we just have to know some expressions, eg for the character
two could well be 3.4 , 5.6 ... therefore the expression for this series that is always character is a digit \\ d this is the expression for a digit
and March April well could be in January and February, so you always know with words I mean you can be [az] and [AZ], here you can take more than an expression but for this example deal \\ w
Therefore our full expression would be this: String
input = "2nd. March-April"
String expr = "(\\ \\ d) o \\ \\ s (\\ \\ w +) - (\\ \\ w +)" The brackets are
because we get three groups that meet with the expression that lies within them.
String input = "2nd. March-April"
String expr = "(\\ \\ d) o \\ \\ s (\\ \\ w +) - (\\ \\ w +)" ;
Scanner s = new Scanner (input);
s.findInLine (expr);
s.match MatchResult result = ();
for (int i = 1; i <=result.groupCount(); i++)
System.out.println (i + "" + result.group (i));
s.close ();
The result would be this:
February 1 March 2 April 3
But what if the input was this:
String input = "40th. March-April"
1 0
March 2
April 3 in group 1 get 0, but we expect a 40, but it is logical
since our expression is only \\ d you expect only one position of a digit, to make them more we would need \\ d +
String input = "40th. March-April";
String expr = "(\\ \\ d +) o \\ \\ s (\\ \\ w +) - (\\ \\ w +)";
Scanner s = new Scanner (input);
s.findInLine (expr);
MatchResult s.match result = ();
for (int i = 1; i <=result.groupCount(); i++)
System.out.println (i + "" + result.group (i));
s.close ();
Results:
January 1940
March 2 April 3
Greetings.
0 comments:
Post a Comment