Util.regex.Pattern documentation
The 1.5 documentation for util.regex.Pattern defines quantifiers that are greedy, reluctant, or possessive. The definitions of these quantifiers seem to be the same. For example, X?, X??, and X?+ are each defined as "X, once or not at all." Is this a mistake? If not, what's that difference among greedy, reluctant, and possessive?
It's not a mistake, it's just incomplete. A normal (greedy) quantifier matches as many times as it can, but will back off if necessary to achieve an overall match. A reluctant quantifier matches the minimum number of times that it has to, and only tries to match more if that's the only way to achieve an overall match. A greedy quantifier matches as many times as it can and never backs off, even if that makes an overall match impossible. Here's a demonstration:import java.util.regex.*;
public class Test
public static void main(String[] args)
String input = "XXXXX";
Pattern p1 = Pattern.compile("(X+)(X+)");
Pattern p2 = Pattern.compile("(X+?)(X+)");
Pattern p3 = Pattern.compile("(X++)(X+)");
Matcher m = p1.matcher(input);
if (m.matches())
System.out.println("p1:\t" + m.group(1) + "\t" + m.group(2));
m = p2.matcher(input);
if (m.matches())
System.out.println("p2:\t" + m.group(1) + "\t" + m.group(2));
m = p3.matcher(input);
if (m.matches())
System.out.println("p3:\t" + m.group(1) + "\t" + m.group(2));
p1: XXXX X
p2: X XXXXIn p1, the X+ in the first group initially matches all five X's, then hands off to the second group. The X+ there has to match at least one X, but there are none left. So the first group gives up one of its X's, the second group matches it, and Bob's your uncle.
In p2, the X+? has to match at least one X, so it does, then hands off to the second group, which happily gobbles up the rest of the input.
In p3, the X++ matches all the X's, but refuses to back off and give the X+ in the second group the one X it needs, so the match fails.
Similar Messages
-
Ava.util.regex.pattern and * - + /
hi...
i'm korean... so I can't speak english.. sorry..^^
but i hava a problem..
import java.util.regex.*;
public class Operator
/ public static void main(String args[])
String operator="/";
////////////////////////////////////////////////////////////// error point..
Pattern pattern=Pattern.compile(operator);
Matcher m=pattern.matcher("- ----* / */* /+");
int count=0;
while(m.find()) {
count++;
System.out.println(count);
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
operator : / - : ok...
operator : * + : error...
i had to use + *..
what's problem??Are you using matches()? Then keep in mind that it requires that the entire String is matched by the RE.
pattern.matcher("about:foobar").matches(); //will return false, as "foobar" is not matched by your pattern
pattern.matcher("about:").matches(); //will return true
pattern.matcher("about:foobar").find(); //will return true
pattern.matcher("notabout:foobar").find(); // will return false -
Java.util.regex.Pattern.error(Pattern.java:1713)
Hi,
I have some users migrating from Toad to SQL Developer.
The following script was correctly running with Toad and not with SQL Developer :
DECLARE
I_DEB DATE;
I_FIN DATE;
BEGIN
I_DEB := to_date('01072010','ddmmyyyy');
I_FIN := to_date('17092010','ddmmyyyy');
MY_SCHEME.MY_PRC ( I_DEB, I_FIN );
COMMIT;
END;
spool c:\temp\out.csv
Select FIELD1 From TABLE1 order by FIELD1;
spool off
SQL Developer is able to run the script in 2 parts :
- 1st without the "spool" section
- 2nd the "spool" section only
But it can't run all the script in the same run.
Is anybody knows this problem ?
Thanks,Try putting a slash(/) after the END:
END;
/ -
Java.util.regex error
Hello,
I checked JavaDoc multiple times but do not see what is wrong with
myString.replaceAll("D:\\web\\mars","")which results in
java.util.regex.PatternSyntaxException: Illegal/unsupported escape squence near index 7
D:\web\mars
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.escape(Unknown Source)
at java.util.regex.Pattern.atom(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at ArticleImageImportProcessor.main(ArticleImageImportProcessor.java:40)
Exception in thread "main" please, every suggestion/hint is most appeciatedYou have to "encode" backslash twice, first for String purpose and second time because of special meaning of '\' in regular expressions.
It should looks like
myString.replaceAll("D:\\\\web\\\\mars","") -
Hi,
Jdev 11.1.1.0.31.51.56
If somebody of you get the following trace stack when running a jspx using ViewCriteriaRow.setOperator :
There is bug 7534359 and metalink note 747353.1 available.
java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
at java.util.regex.Matcher.reset(Matcher.java:291)
at java.util.regex.Matcher.<init>(Matcher.java:211)
at java.util.regex.Pattern.matcher(Pattern.java:888)
at oracle.adfinternal.view.faces.model.binding.FacesCtrlSearchBinding._loadFilter
CriteriaValues(FacesCtrlSearchBinding.java:3695)
Truncated. see log file for complete stacktrace
Workaround:
If you use
vcr.setAttribute("Job",job);
or
vcr.setAttribute("Job","="+job);
than add following line of code:
vcr.setOperator("Job","="); regards
PeterHi,
useful to mention that this happens when setting the equal operator or LIKE operator
vcr.setAttribute("Job","= '"+job+"'");
or
vcr.setOperator("Job","=");
Frank -
RFC used for java.util.regex
Hi,
Does anyone know the RFC used for java.util.regex ??
Thanks & Regards,
Gurushant HanchinalCan you please give me the link to view to specifications for java.util.regex.. I have tried the link which is available in :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html page with name " Java Language Specification"
on click of this link, i am getting page not found error..
Please give me any other alternate links to view the regular expression specifications..
Thanks,
Gurushant Hanchinal -
Regex - Pattern for positive numbers
Hi,
I wanna check for positive numbers.
My code so far:
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
boolean b = m.matches(); But I don't know how to check for positive numbers (including 0).
Thanks
JonnyJust to make your life easier:
package samples;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
* @author notivago
public class Positive {
public static void main(String[] args) {
String input = "- 12 +10 10 -12 15 -12,000 10,000 5,000.42";
Pattern p = Pattern.compile( "\\b(?<!-\\s?|\\.|,)([0-9]+(?:,?[0-9]{3})*(?:\\.[0-9]*)?)" );
Matcher matcher = p.matcher( input );
while( matcher.find() ) {
System.out.println( "Match: " + matcher.group(1) );
} -
Me and my friend pedrofire, that�s probably around forums somewhere, are newbies. We are trying to get a log file line and process correctly but we are found some dificculties to create the right expression pattern.
My log have lines like:
User 'INEXIST' with session 'ax1zjd8yEeHh' added content '769' with uri 'http://mail.yahoo.com/'.
User 'INEXIST' with session 'ax1zjd8yEeHh' changed folder from 'E-mails' to 'Milhagem'.
User 'INEXIST' with session 'a8jXrY_N38ja' updated all content of folder 'Bancos'.
i need to get the following data
USER - [INEXIST]
SESSION - [ax1zjd8yEeHh]
ACTION - [added] or [changed] or [updated].
Getting the user and the session is easy, but i am having difficulties grabing the action, because i need to take just the action word without blank spaces igonring the words content or folder or all.
I m trying this for hours, but to a newbie is a little dificult
Any help is welcome
Thanks
Peter RedmanHi,
How about something like:
import java.util.regex.*;
public class RegexpTest
private static final Pattern p = Pattern.compile(
"^User '([^']+)' with session '([^']+)' ([^ ]+) .*$" );
public static void main( String[] argv )
find( "User 'INEXIST' with session 'ax1zjd8yEeHh' added content '769' with uri 'http://mail.yahoo.com/'." );
find( "User 'INEXIST' with session 'ax1zjd8yEeHh' changed folder from 'E-mails' to 'Milhagem'." );
find( "User 'INEXIST' with session 'a8jXrY_N38ja' updated all content of folder 'Bancos'." );
public static void find( String text )
System.out.println( "Text: " + text );
Matcher m = p.matcher( text );
if ( ! m.matches() ) return;
String user = m.group(1);
String session = m.group(2);
String action = m.group(3);
System.out.println( "User: " + user );
System.out.println( "Session: " + session );
System.out.println( "Action: " + action );
}which results in:
Text: User 'INEXIST' with session 'ax1zjd8yEeHh' added content '769' with uri 'http://mail.yahoo.com/'.
User: INEXIST
Session: ax1zjd8yEeHh
Action: added
Text: User 'INEXIST' with session 'ax1zjd8yEeHh' changed folder from 'E-mails' to 'Milhagem'.
User: INEXIST
Session: ax1zjd8yEeHh
Action: changed
Text: User 'INEXIST' with session 'a8jXrY_N38ja' updated all content of folder 'Bancos'.
User: INEXIST
Session: a8jXrY_N38ja
Action: updatedYou should probably change the Pattern to be less explicit about what it matches. i.e. changes spaces to \\s+ or something similar.
Ol. -
Parsing xhtml using java.util.regex
I am parsing an XHTML file using the java.util.regex package and I am perplexed at why the following doesn�t work.
The lines I wish to match are either like this:
<span class="someclass"><b>Some String.</b></span></td>
or
Some String.</td>
The code I use to try to achieve this is:
Pattern somePattern = Pattern.compile(".*(<span class=\"someclass\"><b>)?(.*)[.](</b></span>)?</td>.*");
String s = null;
while((s = br.readLine()) != null) {
if(somePattern.matcher(s).matches()) {
System.out.println("0:" + eventMatcher.group(0));
System.out.println("1:" + eventMatcher.group(1));
System.out.println("2:" + eventMatcher.group(2));
System.out.println("3:" + eventMatcher.group(3));
I expect to get as output
0:<span class="someclass"><b>Some String.</b></span></td> 1:<span class="someclass"><b>
2:Some String
3:</b></span>
or
0:Some String.</td>
1:null
2:Some String
3:null
depending on which lines provide the match as mentioned above. Instead I get:
0:<span class="someclass"><b>Some String.</b></span></td>
1:null
2:(empty string)
3:</b></span>
or
0:Some String.</td>
1:null
2:(empty string)
3:null
Any ideas? Thanks in advance.Consider the terms of ".*(<span class=\"someclass\"><b>)?(.*)[.](</b></span>)?</td>.*"
.* - greedily collect characters
(<span class=\"someclass\"><b>)? - optionallly collect information taht will always be matched by the previous .* pattern so will be empty.
(.*) - greedily collect characters that will also have been swallowed by the first .* so will be empty
[.] - a single .
(</b></span>)? - optionally collection
</td> - must be there
.* - collect the rest of the charcters in the line.
Therefore in general groups 1 and 2 will be empty because the first .* will have collected the information you wanted to capture!
You could just make the first .* non-greedy by using .*? but this may fail for other reasons.
So, in general terms, what are you trying to extract? -
Doubt in Regular Expressions : java.util.regex
I want to identify words starting with capital letters in a sentence and I want to replace the matched word with "#" added in front of it.... For example, if my input sentence is
"Christopher Williams asked Mike Muller a question"
my output should be,
"#Christopher #Williams asked #Mike #Muller a question"
How do I do that using java.util.regex ?
In perl we can can use *"back references"* in *"replacement string"* . Perl replacement accepts back references whereas java replacement method accepts only strings....
Please help me.....Your replacement is swallowing the space before the uppercase character, and won't match at the beginning of the line.
Also, it's unnecesarily verbose. String has a replaceAll method (that calls the same methods of Pattern and Matcher under the covers)sentence = s.replaceAll("(^| )([A-Z])", "$1#$2");Disclaimer: I'm no prome, sabre or u/a :-) That can probably be simplified.
db -
Regular expressions with java.util.regex
Hello Guys,
I wrote last time this
* Uses split to break up a string of input separated by
* commas and/or whitespace.
* See: http://developer.java.sun.com/developer/technicalArticles/releases/1.4regex/
* Change: I have slightly modified the source
import java.util.regex.*;
public class Splitter {
public static void main(String[] args) throws Exception {
// Create a pattern to match breaks
Pattern p = Pattern.compile("[<>\\s]+");
// Split input with the pattern
String[] result =
p.split("<element attributname1 = \"attributwert1\" attributname2 = \"attributwert2\">");
for (int i=0; i<result.length; i++)
if (result.equals(""))
System.out.println("EMPTY");
else
System.out.println(result[i]);
int res = result.length - 1;
System.out.println("\nStringlaenge: " + res);
I wonder, why I got an empty element in reult[0]. Have anyone an idea?
We'll come together next time
... �nhan Inay ([email protected])What is wrong with this Pattern?
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
This time i used following Split:
p.split("<element attributname1=\"attributwert1\" attributname2=\"attributwert2\">");
I've got a compilation error:
U:\qms_neu\htdocs\inay\Source\myWork\Regex-Samples>javac Splitter.java
Splitter.java:14: illegal start of expression
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
^
Splitter.java:14: illegal character: \92
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
^
Splitter.java:14: illegal character: \92
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
^
Splitter.java:14: unclosed string literal
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
^
Splitter.java:17: ')' expected
p.split("<element attributname1=\"attributwert1\" attributname2
=\"attributwert2\">");
^
Splitter.java:14: unexpected type
required: variable
found : value
Pattern p = Pattern.compile("^<[a-zA-Z0-9_\\"=]+[\\s]*$>");
^
Splitter.java:18: cannot resolve symbol
symbol : variable result
location: class Splitter
for (int i=0; i<result.length; i++)
^
Splitter.java:19: cannot resolve symbol
symbol : variable result
location: class Splitter
if (result.equals("")){
^
Splitter.java:21: cannot resolve symbol
symbol : variable result
location: class Splitter
System.out.println(result[0]);
^
Splitter.java:24: cannot resolve symbol
symbol : variable result
location: class Splitter
System.out.println(result[i]);
^
Splitter.java:25: cannot resolve symbol
symbol : variable result
location: class Splitter
int res = result.length - 1;
^
11 errors -
Does anyone know where to obtain a copy of the java.util.regex.* package (separate package)? This is a new package included in version 1.4.0.
Does anyone know where to obtain a copy of the
java.util.regex.* package (separate package)?
This is a new package included in version 1.4.0.Simple:
Go to your java sdk directory
$ jar xvf src.jar
$ cd java/util/regex
$ javac ASCII.java
$ javac Matcher.java
$ javac Pattern.java
$ javac PatternSyntaxException.java
$ cd ../../..
$ jar cvf regex.jar java/util/regex/*.class
et voila; how difficult can that be ? There is no native, JVM depending stuff in there, although I did not check for dependencies on other new stuff inside the jdk.
I'm also unaware if this isn't illegal under the agreement with Sun. -
Patters for avoiding the '/' (in java.util.regex)
Hi all ,
i wanted to knw the pattern format (in java.util.regex) for avoiding the '/' at the begenning of a line ....... hope to get the best answers ....
thnx 'n cheers
maxfirst of thnx for the quick responses .... !
what i am really trying to do here is to avoid all lines which are starting with a comment ( '//' or '/*' ) .
-> I am checking for all lines and inserting a line of code just before a particular statement , the only thing is that statement which i am looking for should not be inside a comment ( '//' or '/*' ) .... hope u got the thing
thnx
max -
About the error of java.util.regex in jdk1.4's docs
In java.util.regex,the class Pattern's document says:
Greedy quantifiers
X? X, once or not at all
X* X, zero or more times
X X, one or more times
X{n} X, exactly n times
X(n,} X, at least n times
X{n,m} X, at least n but not more than m times
Why don't metion �X+�?
I think that should be "X+ X, one or more times",right?Agreed. I use Regex in many places (and used
Oromatcher before 1.4), and I've verified that I
use the + operator in several places-- it works. -
Please help on java.util.regex.*
Hi all,
My RTF file looks like this:
Project Num\tab N/A
\par Project Name\tab Hook-up Installation and Service
\par
My intension is to read the file until the \tab and store Project Num as a string into a
variable. Similarly read until \par and store the value of Project Num into another variable.
So that i can use those variables further in my program.
I used java.util.regex.* package for this purpose. I could successfully split the sentence whenever it sees \tab and \par but don't know how to get the text before and after the delimeters and store them into variables.
The code which i wrote is:
import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;
public class RegexDemo{
public static void main(String[] args){
// Create a pattern to match breaks
Pattern p = Pattern.compile("\\\\.[a-z][a-z]",Pattern.DOTALL);
try{
File file = new File("sample.rtf");
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
// Get a CharBuffer from the source file
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
Charset cs = Charset.forName("8859_1");
CharsetDecoder cd = cs.newDecoder();
CharBuffer cb = cd.decode(bb);
// Run some matches
Matcher m = p.matcher(cb);
while (m.find())
System.out.println("Found comment: "+m.group());
}catch(Exception e){
e.printStackTrace();
Please somebody help me in this regard. I have spent lot of time searching the forums but couldn't find any solution.
Thanks in advance
rnalluJust put target inside parenthesis with delimiters at boundaries.
Example: "(\\w+)\\t(\\d)\\s" will match occurrences of a word followed with a tab char then a digit followed with a whitespace. If target string matches pattern then m.group(1) contains the word and m.group(2) contains the digit.
Maybe you are looking for
-
How to use iSight for webstreaming video
hello, recently my wife and I purchased a dog(mini bulldog). she is obsessed with the dog. the dog stays home durring the day. she would like for me to place our laptop in the dog room to provide a live video feed. is there any possible way to do thi
-
Printout of PR and Reservation
Hi I wanted to check if we can take print of Purchase Requisition and Reservation as per standard SAP. If we can take can somebody let me know the TransactionCode for the same? Regards
-
Consuming HTTPS based external service
Hello, I am trying to generate consumer service proxy, for a HTTPS based service within the same organization, but I am trouble generating the proxy. Below is the error from task bar. *Exception occurred in communication framework:Error in HTTP Frame
-
i was restoring my itouch and after completed, the itouch screen has a "USB arrow to iTunes" picture. Now iTunes does not see the itouch and my itouch is non-responsive
-
Touch Screen Not Working At Times
I am using the Lumia 900. There are times in the afternoon, when I go to check something on the phone, I can touch any of the app buttons on the screen and nothing happens... when I click the button to wake it up, I cannot make the wallpaper pictu