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

Similar Messages

  • Who use sql-mapping with java.util.regex?

    Hi everyone:
    I use the IBatis SQL-Mapping and I think it is very good.Now I want to add the search function to my BBS forum.I also want to display the content high light like jive.I mean that if I want to find the string "ibatis",then the search result "ibatis" will be high light displayed.
    So I must use the java.util.regex in jsdk1.4.But the problem is that what I get is a List if I use sql-mapping.For example:
              String resource="conf/XML/sql/lyo-sql-map.xml";
              Reader reader=Resources.getResourceAsReader(resource);
              sqlmap=XmlSqlMapBuilder.buildSqlMap(reader);
              List articlelist=sqlmap.executeQueryForList("selectSiteArticle","%"+icontent+"%");
    The result I get is a List and I have no time to use regex.
    I don't know whether I could do this:
    Iterate the List,use the regex and later place all the object back to the List.
    It's right?
    How to use regex with sql-mapping?Thks

    Any idea? :(

  • Regular Expressions with Java Regex

    Hi,
    I'm playing around with regex and there's something I can't get to work. What I need, is to capture words between 2 other words and the words captured has to be higher than 5 characters, so for example:
    Pattern "Just testing on something with regular expressions" and suppose I'll try to match all the words between "testing" and "regular", then only the word "something" should come out because "on" and "with" are not larger than 5 chars.
    Now I'm quite new to regexps and I know that ((?<=\btesting\b).*(?=\bregular\b)) will return " on something with "
    But I can't seem to come up with an expression that would only output the word "something". I've tried a few expressions like ((?<=\btesting\b)((?:[\s\w{1,3}])*(\b\w{4,}\b)*(?:[\s\w{1,3}])*)*(?=\bregular\b)) which also returns " on something with " The others I tried would either return the whole " on something with " or return "Not Found!"
    Does anyone have a tip for me? I'm well aware that it's not too hard to do something like this in Java, but I'm really looking to study regular expressions and would like to accomplish this using a regular expression.
    The Java program I use is the following:
    C:\Program Files\Java\jdk1.5.0_16\bin>java RegexTest "((?<=\btesting\b).*(?=\bregular\b))" "Just testing on something with regular expressions"
    public class RegexTest {
         public static void main(String[] args) {
              Pattern RegexCompile = Pattern.compile(args[0]);
              Matcher m = RegexCompile.matcher(args[1]);
              boolean found = m.find(); // Perhaps there's another function to find () that would do the job?
              if (found)
              System.out.println(m.group()); // Perhaps group() is not the right function for this case?
              else
              System.out.println("Not Found!");
    Edited by: dli2k3 on Sep 19, 2008 11:32 AM
    Edited by: dli2k3 on Sep 19, 2008 11:33 AM

    You're talking about a two-stage operation: find everything between those two words, then filter out anything that's less than five letters long. There's no single regex that will accomplish all that in one step.
    By the way, please use &#x7B;code} tags when you post source code.

  • Regular expression in java -- specifically email

    Does anyone know how I can do a regular expression with java that is going to retrieve an email address pattern.
    For example, let's say i have a huge string
    this is a sample test. perhaps someoen can tell me how to retrieve my [email protected] from this string. sincerely yours [email protected]
    could someone explain to me how to retrieve these emai addresses most efficiently using java's regular expressions
    thanks
    stev

    A citing (http://jregex.sourceforge.net/examples-email.html):
    String someValidChars="[\\w.\\-]+";
    String someAlphaNums="\\w+";
    String dot="\\.";
    Pattern email=new Pattern(someValidChars + "@" + someValidChars + dot + someAlphaNums);

  • Regular Expressions (java.util.regex)

    I am developing using a product that must
    use java 1.2.2_05a but I want to use regular
    expressions, does anybody know where I can
    get of the package java.util.regex without
    having to download the whole java 1.4 release.
    Or does someone know of an alternative that
    I can use ?

    There is another regex pack for java available from Apache Foundation Project. You can try it.
    Take a look at http://jakarta.apache.org/

  • Java.util.regex  vs jarkata-oro on regular expression

    hi, all
    Both java utility (java.util.regex) and jarkata-oro (org.apache.oro.text.perl) provide supports of Perl5 compatible regular expressions. What are your opinions of which one do you choose?
    I've searched in this forum and others online. I don't see this topic. Maybe I miss something...
    Anyway, your opinions/experences are more than welcome.
    thx
    Tim

    One big difference:
    java.util.regex is JAVA 1.4+ only.
    jarkata-oro is JAVA 1.2+

  • 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

  • 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 appeciated

    You 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","")

  • Oracle Regular Expressions in Java?

    JDEV 10.1.3
    ADF BC
    ADF Faces
    Is there a Java library for dealing with Oracle regular expressions? I would like to be able to test a String against an Oracle regular expression for pattern match.
    Thank you,

    Hi,
    Java supports RegularEpressions: http://www.exampledepot.com/egs/java.util.regex/pkg.html
    Frank

  • RFC used for java.util.regex

    Hi,
    Does anyone know the RFC used for java.util.regex ??
    Thanks & Regards,
    Gurushant Hanchinal

    Can 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

  • "Stupid design of regular expression in java"

    I bet majority will stumble on this:
    String inputStr = "a,,b";
    String patternStr = ",";
    String[] fields = inputStr.split(patternStr);
    result: ["a", "", "c"]
    so if inputStr = ,,
    then result: ["","",""] ??
    If you think so, give yourself a treat.
    Now give yourself a bigger treat. cause
    the result is [""]
    Unbelievable about inconsistency of regular expression.
    It look so irregular.

    Take a look at the site which quote
    http://www.devarticles.com/c/a/Java/Regular-Expressions/10/
    Perl is probably the most popular language to offer regular expression support. As such, it makes sense to put Java&#8217;s regex support in context by comparing it to that of Perl. The distinctions you should be aware of are highlighted in the sections that follow. Generally speaking, J2SE doesn&#8217;t include some Perl constructs, because Java is a full-featured programming language that offers sophisticated condition and logical paths of execution that are reasonable alternatives to the constructs offered by Perl."
    Since, Java doesn't offer means it is perl, why not fix the "weirdness" of perl is in this case ? let the split function work as intended.
    Check this out from Java almanac
    // Parse a comma-separated string
    String inputStr = "a,,b";
    String patternStr = ",";
    String[] fields = inputStr.split(patternStr);
    // ["a", "", "b"]
    // Parse a line whose separator is a comma followed by a space
    inputStr = "a, b, c,d";
    patternStr = ", ";
    fields = inputStr.split(patternStr, -1);
    // ["a", "b", "c,d"]
    // Parse a line with and's and or's
    inputStr = "a, b, and c";
    patternStr = "[, ]+(and|or)*[, ]*";
    fields = inputStr.split(patternStr, -1);
    // ["a", "b", "c"]
    Everything is mentions except to solve the common issue I have.
    http://javaalmanac.com/egs/java.util.regex/ParseLine.html
    The above will failed if any empty data within pipe. It is how you manage to detect the empty data that is relatively important because it screw up your program. And too code to do that increase your chance of program error.
    Sometimes we just accept thing but never question why there aren't better way.

  • Can I use regular expressions in Java 1.3

    Hi,
    Dose Java 1.3 suport regular expressions?
    How can I use it?
    Thanks.
    bevin ye

    The 1.3 core API doesn't support regular expressions. Hint: There's an item "Since:" in the JavaDoc of most classes that indicates the version it was initially available. If you look it up in the JavaDoc of java.util.regex.Pattern you'll notice that it's value is 1.4.
    But there are several third party libraries that implement regular expressions, 'though I've not used them extensivly, so I can't tell you which one's the most usefull.

  • Perl Regular expression to java Regular Expression

    HI all,
    How can i write java Regular expression for the below Perl Code
    where data.html is my original Html file
    and data2.html is output file.
    open(FPR, "data.html") || die("Could not open data file");
    while ($line=<FPR>) {
    $content .= $line;
    close(FPR);
    open(FPR, ">data2.html") || die("Could not open data2 file");
    # clean white spaces
    $content =~ s/[\n\r\0 ]//g;
    # divide data by td
    $rxp='<tr.*?><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><td.*?>(.*?)<\/.*?td><\/.*?tr>';
    while ($content=~ m/$rxp/g)
    print FPR "\n".$1."\t".$2."\t".$3."\t".$4."\t".$5."\t".$6."\t".$7."\t".$8."\t";
    print FPR "<br>";
    close(FPR);
    can you help in this regard
    Thanks

    I am able to retrive only one row in this format from data.html file
    <trvalign=middlebordercolor=#ffffff><tdwidth='40'CLASS='tdbgpricespagecolorgrey'><fontface='Arial,Helvetica,sans-serif'size='2'>SB</font></td><t
    dwidth="23"Class=tdbgpricespagecolorgrey><fontface='Arial,Helvetica,sansserif'size='2'>USAirways</font></td><tdwidth="34"Class=tdbgpricespagecolorgrey><fontface='Arial,Helvetica,sans-serif'size='2'>MIA</font></td><tdwidth="31"Class=tdbgpri
    cespagecolorgrey><fontface='Arial,Helvetica,sans-erif'size='2'>LGW</font></td><tdwidth="23"Class=tdbgpricespagecolorgrey><fontface='Arial,Helvetica,sans-serif'size='2'>USAirways</font></td><tdwidth="34"Class=tdbgpricespagecolorgrey><fontface='Arial,Helvetica,sans-serif'size='2'>LGW</font></td>
    But i need the output in this format
    <fontface='Arial,Helvetica,sans-serif'size='2'>SB     <fontface='Arial,Helvetica,sans-serif'size='2'>USAirways     <fontface='Arial,Helvetica,sans-serif'size='2'>MIA     <fontface='Arial,Helvetica,sans-serif'size='2'>LGW     <fontface='Arial,Helvetica,sans-serif'size='2'>USAirways     <fontface='Arial,Helvetica,sans-serif'size='2'>LGW     <fontface='Arial,Helvetica,sans-serif'size='2'>MIA          <br>
    <fontface='Arial,Helvetica,sans-serif'size='2'>CS     <fontface='Arial,Helvetica,sans-serif'size='2'>USAirways     <fontface='Arial,Helvetica,sans-serif'size='2'>MIA     <fontface='Arial,Helvetica,sans-serif'size='2'>LON     <fontface='Arial,Helvetica,sans-serif'size='2'>USAirways     <fontface='Arial,Helvetica,sans-serif'size='2'>LON     <fontface='Arial,Helvetica,sans-serif'size='2'>MIA          <br>
    How can i rewrite the code to achive this.
    Here is my java code
    import java.io.*;
    import java.util.*;
    import java.util.regex.*;
    public class parseHTML {
    public static void main(String[] args)
    try
    BufferedReader in = new BufferedReader(new FileReader("C:\\data.html"));
    PrintWriter out = new PrintWriter(new FileWriter("C:\\data1.html"));
    String aLine = null;
    String abc=null;
    String pattern1 ="<tr.+?><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td><td.+?>(.+?)</.+?td>++";
    Pattern p1 = Pattern.compile(pattern1);
    while((aLine = in.readLine()) != null)
    abc=aLine.replaceAll("(\n|\t|\r)","").replaceAll(" ","");
    Matcher m1 = p1.matcher(abc);
    if(m1.find())
    System.out.println("the value is...."+m1.group());
    out.print(m1.group());
    m1.reset(aLine);
    in.close();
    out.close();
    catch(IOException exception)
    exception.printStackTrace();
    Thanks

  • Using regular expressions in java

    Does anyone of you know a good source or a tutorial for using regular expressions in java.
    I just want to look at some examples....
    Thanks

    thanks a lot... i have one more query
    Boundary matchers
    ^      The beginning of a line
    $      The end of a line
    \b      A word boundary
    \B      A non-word boundary
    \A      The beginning of the input
    \G      The end of the previous match
    \Z      The end of the input but for the final terminator, if any
    \z      The end of the input
    if i want to use the $ for comparing with string(text) then how can i use it.
    Eg if it is $120 i got a hit
    but if its other than that if should not hit.

  • WHERE to find [b]java.util.regex.*[\b] package?

    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.

Maybe you are looking for

  • Configurar SAPROUTTAB para permitir acesso de fora rede interna

    Bom tarde pessoal,   Gostaria de saber, como faço para configurar a rota no saprouttab para liberar o acesso externo via front end?   Ex:   O usuário na sua casa, configura o front end com o ip publico e o ip do servidor interno da organização e o me

  • Lock when trying to update a Z-field in MARD

    Hi, I have a Z-field in an append to MARD. In MIGO we create an output idoc (a custom made Z-idoc) and in the function module processing the idoc I want to update my Z-field in MARD. The update in MARD are made with a bapi called BAPI_MATERIAL_SAVEDA

  • Apple TV (A1469) Restore Fail

    I woke up yesterday morning to find my Apple TV (A1469) needed restoring. I was a little worried but had a day of lecturers so I said to myself that I would deal with the problem last night. Needless to say after 6 restore attempts last night I gave

  • Calendar Patch 2007 Update Offline

    Where can I find the Calendar patch 2007 file for offline update. By USB. It solved my sync trouble whit my 1st Curve 8300 but not on my new Curve 8310. The web page said (updated completed) but I don't see it in my list of application. And  I still

  • Cross docking in LE-TRA

    Hello Experts, Can anyone help to customize the functionality of Cross docking in LE-TRA? Does any one have any customize guide of this functionality? Any power point presentation with examples or some training exercises with real examples. I will be