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
Similar Messages
-
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. -
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","") -
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? -
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 -
Patterns and Matcher.find()
I would be really grateful for some assistance on this. I have been at this for three hours and can't get it correct.
I have a string such as this: "Hello this is a great <!-- @@[IMAGINE_SPACIAL_VECTOR]@@ --> little string"
I want to use RegEx, Pattern and Matcher.find() to extract "IMAGINE_SPACIAL_VECTOR" ie. the text between "<!-- @@[" and "]@@ -->"
Thanks in advance for your helpimport java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String data = "Hello this is a great <!-- @@[IMAGINE_SPACIAL_VECTOR]@@ --> little string";
Matcher matcher = Pattern.compile("<!-- @@\\[(.*?)]@@ -->").matcher(data);
while (matcher.find()) {
System.out.println(matcher.group(1));
}Might not be the best way, but it works.
Kaj -
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. -
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 -
Pattern and Regular Expression problem
Hi all!
I have trouble with constructing a pattern.
Consider the following:
import java.util.regex.*;
public class Parser {
public Parser() {
String pattern = "<i18n:message(.*)/>";
String myline = "<INPUT type=\"button\" name=\"cancel\" value='<i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>' class=\"normal08\" onClick=\"javascript:disableInput();document.panel_menu.submit();\"/>";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(myline);
while ( (m.find()) ) {
System.out.println(m.group());
public static void main(String[] args) {
new Parser();
How should I construct the pattern in order to extract from the string myline just the following sequence: <i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>
It seems that using the pattern I wrote, it continues the match until the last appearance of "/>", and I need to have the match done until the first appearance of "/>".
Thanks a lot.No, modify it to (.*?).Hey, unky. Would you mind schooling me a bit here?
"<i18n:message(.*?)/>" right?
against "<i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>' class=\"normal08\" onClick=\"javascript:disableInput();document.panel_menu.submit();\"/>"
I think I understand, but would appreciate confirmation or correction.
.* is zero or more of anything. The ? makes it reluctant (also called lazy?).
So .* is compared against nothing, succeeds, and the regex moves on to the next character.
This doesn't match / so regex backtracks and tries to match .* against the space, which succeeds, but then / doesn't match against the k in key
So we backtrack, and reluctantly add the k to what .* matches.
We keep adding a character to .*'s match every time the next character isn't a /.
Eventually, .* matches everything up to and including the quote before the first />.
At this point, the / and > literals match, and we're done.
Yes?
And we needed to make it reluctant, else the .* would have started by matching as much of the string as it could, gobbling up all of it, and then backtracking to give the /> literals their match at the end of the string.
Yes?
Thanks! -
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 -
RegEx: find() and group() issue...
Hi,
In the code below, I use the find() method to search the first occurence of a match and then group(1) to retrieve that match if the find() method returns true.
The problem is that the find() method returns true and the group(1) throws an ArrayOutOfBoundException :(
Here is the RegEx code I use:
String value = "${local.production}\\makeresults\\repository";
Matcher matcher = Pattern.compile("\\$\\{.*\\}").matcher(value);
if (matcher.find()) {
System.out.println(matcher.group(1)); // throws an ArrayOutOfBoundException
Got this output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 1
at java.util.regex.Matcher.group(Matcher.java:463)
at Test2.main(Test2.java:28)Have I made any mistakes?
Thanks in advance for your help.
Best regards,
Serge.You shall use group() without parameter to get complete sequence.
Only capturing groups (between parenthesis) are indexed (from left to right, starting at one.)
Here you don't have capturing group (and that's why it says No group 1), and it seems that you need the entire pattern, so you need the group() method.
Matcher
Tim - Actually, you can also use group(0) ...
Maybe you are looking for
-
Hy, everybody I try to activate BI content (BI 7.0). I Selected Install in Background. In SM37 transaction i see that process is canceled. I don't know there is a problem. Could you help me? Thanks in advance. Best Regards, Arunas Stonys
-
White screen, not syncing with computer- tried holding buttons please help?
Please help. The screen of my daughters 4th gen ipod has frozen white. Have tried syncing the frozen ipod with computer but it is not recognised. I have treid holding home and sleep button at the same time but no change. Would be prepared to do a res
-
In my hp 4530 one speaker is not working.?
when i was listening music one speaker had been disappear. plz tell me soltion . i have install driver again the problem not solved
-
Price Updation of Finished Good
Dear Experts, Can any one explain how the MAP of a material (SG/ SFG) whose price indicator is standard price gets calculated in system. I Have a material CLINKER with price indicator Standard Price and the price is fixed as 145. In MM03 the MAP of t
-
ResourceBundle with Database backend
Hi all, We're busy creating a web application that needs different localization. ResourceBundles would probably be the best thing to use for this. However, we would like to use a database as the backend for the ResourceBundle. This would make changin