Java.util.regex and replacing patterns with function calls

Hi everyone,
I'm in terrible need for help.
Any advice is much appreciated.
I have the following sentence in a file. The sequence of numbers is actually a
date in seconds since 1970. I need to identify the 9 or 10 sequence of numbers
send that to a method that will transate the numbers into a date in a string format.
Then replace it in the file.
Here is an example:
"My name is Peter. Please 1020421277 help me figure this out 108062327. "
using the following block I can convert it to this
"My name is Peter. Please | help me figure this out |. "
[block]
Pattern p = Pattern.compile("[0-9]{9,10}+");
Matcher m = p.matcher("");
String aLine = null;
while((aLine = in.readLine()) != null) {
m.reset(aLine);
String result = m.replaceAll("|");
out.write(result);
out.newLine();
[end block]
So I need to change the above block so that my sentence looks like this.
"My name is Peter. Please 05/03/2002 10:21:17 help me figure this out 06/04/1973 17:18:47. "
The method that converts the numbers into a date is not of a concern because I already have
the code to do that.
If anyone has suggestion, please let me know.
Thanks in advance.
Peter
[email protected]

Never mind, I was able to figure it out....
Common2 cm = new Common2();
Pattern p = Pattern.compile("[0-9]{9,10}+");
Matcher m = p.matcher("");
m = p.matcher(s1);
while ((found = m.find())) {
String replaceStr = m.group();
System.out.print("\tReplaceString is: " + replaceStr);
replaceStr = cm.get_date(replaceStr);
System.out.println("\t\tConverted to: " + replaceStr);
m.appendReplacement(buf,replaceStr);
m.appendTail(buf);
----------------- The get_date method in Common2 looked like this ------------------------------
public String get_date(String myString)
String s;
long lsecs = Long.parseLong(myString);
lsecs = lsecs * 1000; // its now milliseconds.
// Determine default calendar and then the offset to GMT.
Calendar localCalendar = Calendar.getInstance();
int offset = localCalendar.get(Calendar.DST_OFFSET) + localCalendar.get
(Calendar.ZONE_OFFSET);
// Take the number of milliseconds subtract the gmtoffset
Date GMTdate = new Date(lsecs - offset);
// Format date
Format myformat;
myformat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a z ");
s = myformat.format(GMTdate);
return s;
Peter

Similar Messages

  • HT202912 How do I remove Java 1.7 and replace this with Java 1.6.0_65?

    I need to get back to  an older JRE specifically Java 1.6.0_65. Right now I have Java 1.7.-_71-b14.
    I've tried the instruction in the terminal - sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
    That doesn't work. In Terminal mode, a "java -version" query returns the same "Java 1.7..." indicating it has not been removed. What else do I have to do here ?
    Thanks

    I need to get back to  an older JRE specifically Java 1.6.0_65. Right now I have Java 1.7.-_71-b14.
    I've tried the instruction in the terminal - sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
    That doesn't work. In Terminal mode, a "java -version" query returns the same "Java 1.7..." indicating it has not been removed. What else do I have to do here ?
    Thanks

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

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

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

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

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

    first 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

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

    Just 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.

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

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

  • [bug]Jdev 11g:NullPointerException at java.util.regex.Matcher.getTextLength

    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
    Peter

    Hi,
    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 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

  • -refurbished iMac has a 20GB partition for Boot Vamp with FAT format.  I want to remove this and replace it with a much bigger format with NTFS formats?

    My (new to me) Apple refurbished iMac has a 1TB hard drive. Running OS 10.6.7 Snow Leopard.  When I tried to install a Boot Camp partition, prior to installling Windows 7- 64 bit, I discovered that the machine had a 20 GB Boot Camp partition, too small for what I need.  HOw do I remove the 20 GB partition, and replace it with a 500 GB partition?

    I have a number of PC and Mac Pro running Windows Pro, and the OS only needs 80GB partition, plus whatever data etc.
    New but used does not sound like Apple Store (refurbished) Specials which would be a 'virgin' OS.
    I would reformat and do your own clean install.
    There may be something wrong and why it was left that way for one thing. May be a problem with the partition table.
    Boot from OS X DVD and you can usually try Disk Utility to "-" remove and then repair and get back, and you of course normally should be able to just use Boot Camp Assistant which is one of its functions - not just to create partition.
    For normal resizing, or creating the free space you need has to be in one contiguous chunk and not be fragmented, which is hard to imagine that 500GB is going to be intact.
    And whatever you do, rule #1 in the pdf guide: backup before beginning.
    http://www.apple.com/support/bootcamp

Maybe you are looking for

  • Interactive Report Customization

    Hello, I need help in customizing the interactive report. for example: my report IR shows the data as below: Department Name Department Number sales 10 service 20 HR 30 Finance 40 I would like to customize the report such that the report shows the da

  • Help with styling radio buttons

    Hi can anyone tell me if its possible to add a vorder colour and a background to the selected radio button and text on this website http://62.6.176.22/products/leaflets.aspx?Id=145399&Key=F8FA69A7-4AC5-4986-97F3-6C8C9520FA 87. This is a site which is

  • Should i update my iphone 4 (not iphone 4s) to ios7.12 or something similar? thanks

    hey all should i update my iphone 4 (not iphone 4s) to ios7.12 or something similar? thanks

  • Imac i7 and Leopard

    Hi everybody, is there any problems with downgrading with Leopard on a 27" i7. The cause is Pro Tools and his (very bad) beta snow leopard version. I heard about Airport problems. Has anyone tested new iMac with 10.5.8? Thanks

  • Can we change key field values in table maintenance

    Hi,   I have created Z-table and table maintenance for the same... In table maintenance..Can we change key field values?? Is there any posibility???? Rayudu