Capture regex match as a variable?

Hello!
I have this program and I basically want to match a part of a string and grab the match as a variable. In this case, the string I need to parse is 'foo'.
Here is what I have:
public class Test
     public static void main(String[] args)
                // link <link> format
          String foo = "http://www.foo.com <http://www.foo.com>";
          String the_regex = "\\<(http://[^\\>]*)\\>";
          String the_replacement = "<a href=\"$1\">$1</a>";
          System.out.println(foo.replaceAll(the_regex,the_replacement));
}$1 (sorta like PERL) should be the captured text from the_regex
Any ideas?
Thanks in advance.

Dubwai - I think I got it, thanks for the guidance. Here is what I used, and it seems to work. Thanks!
public class Test
     public static void main(String[] args)
          String foo = "http://www.foo.com <http://www.boo.com>";
          String regex="\\<(http://[^\\>]*)\\>";
       Pattern p = Pattern.compile(regex);
       Matcher m1 = p.matcher(foo);
       while (m1.find())
         System.out.println("The site = " + m1.group(1));
}

Similar Messages

  • How can I ensure that the audio sample rare of my capture preset matches?

    Hi everyone
    When capturing tapes I get warning that the audio sample rate of one or more of captured files does not match the sample rate on my source tape. This may cause the vidio and audeo of the media files to be out of sync. How can I make sure that the my capture preset matches the sample rate of my tape? Can anyone be able to show me how? Thank you. Faruk.

    Hi
    Fuerther, I have double checked and found that none of my ten projects has sound, although audio meters settings moves up & down. Simultaniously, canvas displays that in- & out of clips are not set , and in browser I see time codes on the images when the playhead stops in timeline. I did not have this problem before. I wonder appreciate if if these issues are interrelated, and if I may have clicked something that has triggered this.
    I would appreciate it if you or other friends could kindly address this problem and help me resolve the isssue. Thank you. Faruk.

  • Question on regex Matcher (group number)

    HI, everybody
    I am writing a program on replacement like the one below.
    String regex = "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)";
    String original = "ABCDEFGHIJKL";
    String replacement = "$12";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(original);
    String result = m.replaceFirst(replacement);What I actually want is to take out the first group, in this case an "A", and append a character "2" after it.
    The result I am expecting is "A2". But the result I get is "L". For the regex engine takes it as the 12th group.
    What should I do to remove the ambiguity.
    Thanks.

    In such case, use $1\\2.

  • Regex matching bug?

    it seems like j2sdk1.4.2b has some serious regex matching bug with strings that contain unicode characters. In my case, the string contained some Turkish chars.
    regex is simple <[^>]*> which matches string runs that are enclosed in <>
    (ex. <field>)
    although the matching is successful with j2sdk1.4.1_02, it just doesn't match unicode containing text with 1.4.2b
    What do you think? Is this a bug or could I be missing something?

    ahmeti, did you submit a bug report on this? Because it definitely is a bug in the Pattern class, I finally figured out. They added a new node type to make matching ASCII characters in character classes more efficient, but they screwed up the match condition: it always returns false if the character it's looking at is not ASCII, even if the class has been negated. I'll go ahead submit a report myself unless I hear from you.

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

  • Regex Matching Involving Unicode

    Hi,
    I'm trying to do a regex match using boost::regex and followed the instructions on http://niemannross.com/developer/wiki/index.php?title=Using_boost_regular_expressions_(re gexp)_in_InDesign_CS/CS2/CS3_plug-in_code
    My regex needs to match a line that ends with punctuation characters and return the string that excluding the ending punctuation characters.
    ex. home -> home
    ex regex: (.*?)[ \\x{201C}\\x{201D}]+$
    however it does not match the line.
    I tried using boost::u32regex but i'm getting a boost::icu_regex_traits::translate_nocase symbols not found error on linking.
    How can I go around this problem?
    Thanks in advance!
    -- Jeff

    Escaping the backslash in '\x' is necessary for your programming language, otherwise it is interpreted as a 'real' hex character. So as it is, this feeds '\x{201C]' into your program, rather than the literal 0x201C code. (It'd be a syntax error for C, but you get the point.)
    However: because this is an expression IN GREP inside your running program, I think you have to escape it again, so it might need double double backslashes. Scripts suffer the same problem.

  • Sequence and Capture Settings Match, Still Need To Render

    I am capturing my video footage through a AJA Kona board using the SDI input. I have set my capture setting in FCP to 525 29.97 DV, and have set my sequence setting to match, also 525 29.97 DV. Yet when I overwrite the footage to the the sequence it needs to be rendered. I was under the impression that if the sequence and capture settings match I would not have to render. Is this not true?

    The orginal footage is not DV, it is DVCPRO 25. I am capturing in the DV setting because of the length of the footage. The DVCPRO deck doesn't have a firewire output.

  • Regex Matching on Capture groups

    I have this regular expression:
    (throw|give)(?: ([1-3][A-B]))+
    given this input:
    throw 1A 2C 1B 3C
    How would I capture each of the items 1A 2C 1B and 3C?
    In the above expression I have 3 capture groups
    group0: whole expression
    group1: (throw|give)
    group2: ((?:1|2|3)(?:A|B|C))
    The problem is that when I execute the find() on my matcher it tries matching the whole expression at once! That means for the group 2 I always get the last match only:
    group0: throw 1A 2C 1B 3C
    group1: throw
    group2: 3C
    How do I get the matcher to only match on ONE capture group at a time?! Is it possible? I thought that was the purpose of the find() method. The documentation says find() matches on a "subsequence", yet I can only get it to match on the whole expression. Plus, I don't see where "subsequence" is defined in the documentation. What am I missing here?

    "Attempts to find the next subsequence of the input sequence that matches the pattern." (my emphasis)
    find() matches the whole regex, not components thereof. What you need to do is use one regex to match the whole expression and return a capture group with the digit-letter pairs, and then use another regex on that capture group to extract the pairs one at a time.*******************************************************************************
    Answer provided by Friends of the Water Cooler. Please inform forum admin via the
    'Discuss the JDC Web Site' forum that off-topic threads should be supported.

  • REGEX : Mapping matches to existing variables

    I need to extract values from strings of the form
    "$PARAM_NAME[1,5,7]$"
    where PARAM_NAME can contain any letters and numbers and underscores.
    For the example above I would like to map existing variables a, b & c as follows.
    a=1
    b=5
    c=7
    There was a way to do this PERL with parenthesis -I believe. I understand Java is not PERL but I wonder what's an effective way of doing this with Java REGEX
    facilities?
    Many Thanks in Advance!

    maybe(?) something like this
        String str = "$PARAM_NAME[1,5,7]$";
        String[] abc = str.replaceAll(".*\\[|\\].*","").split(",");
        for(int x = 0; x < abc.length; x++) System.out.println(abc[x]);

  • Util.regex matcher.groupCount()

    Hello all. I am trying to parse some text using regex. What I am parsing may have 1 or more matches per line and I need access to each match independantly. The code shown below works well in finding all matches except for the m.groupCount() always returns 0. Thus I can't to anything with individual matches. How can get the groupCount() to function properly?
    Thanks in advance.
    f (currentLine.startsWith("LOCUSLINK")){
                      line++;
                      String pattern = "[0-9]+";
                      Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
                      Matcher m = p.matcher(currentLine);
                      while(m.find()) {
                        int count = m.groupCount();
                        for (int x = 0; x <= m.groupCount(); x++)
                        System.out.println(line+"="+x+"="+count);

    There aren't capturing groups and really don't need to use in this case.
    Try this simple way:
    String re = "\\d+";
    Matcher m = Pattern.compile(re).matcher(anyString);
    for (int j=1; m.find(); j++) {
    System.out.println("matching " + j + ": " + m.group(0));
    ..

  • Capturing the Value in the Variable

    Dear Friends,
    There is a requirement to capture the value of the document date while the report is getting executed.  i.e., for each and every record in my report has different dates, my variable should get populated with the document date for every record.......How can i achieve the same.
    Thanks
    Prem

    Hi Prem,
    You need to crate a user exit varable in your query and  write a abap code to capture value and  fill it.
    Thanks,
    Ravi

  • How to replace regex match into a char value (in the middle of a string)

    Hi uncle_alice and other great regex gurus
    One of my friends has a peculiar problem and I cant give him a solution.
    Using String#replaceAll(), i.e. NOT a Matcher loop, how could we convert matched digit string such as "65" into a char of its numeric value. That is, "65" should be converted into letter 'A'.
    Here's the failing code:
    public class GetChar{
      public static void main(String[] args){
        String orig = "this is an LF<#10#> and this is an 'A'<#65#>";
        String regx = "(<#)(\\d+)#>";
        //expected result : "this is an LF\n and this is an 'A'A"
        String result = orig.replaceAll(regx, "\\u00$2");
        // String result = orig.replaceAll(regx, "\\\\u00$2"); //this also doesn't work
        System.out.println(result);

    I don't know that we have lost anything substantial.i think its just that the kind of task this is
    especially useful for is kind of a blind-spot in the
    range of things java is a good-fit for (?)
    for certain tasks (eg process output munging) an
    experienced perl programmer could knock up (in perl)
    using built-in language features a couple of lines
    which in java could takes pages to do. If the cost is
    readability/maintainability/expandability etc.. then
    this might be a problem, but for a number of
    day-to-day tasks it isn't
    i'm trying to learn perl at the moment for this exact
    reason :)Yes. And when a Java source-code processor(a.k.a. compiler) sees the code like:
    line = line.replaceAll(regexp,  new String(new char[] {(char)(Integer.parseInt("$1"))}));or,
    line = line.replaceAll(regexp,  doMyProcessOn("$1")); //doMyProcess returns a Stringa common sense should have told him that "$1" isn't a literal string "$1" in this regular expression context.
    By the way, I abhor Perl code becaus of its incomprehensibleness. They can't be read by an average common sense. Java code can be, sort of ...

  • Regex - matching literal characters

    Im trying to match the following pattern using regex:
    The string begins with a literal '\' is followed by any number of letters and/or numbers and ends with '&0]'
    e.g. '\07761739009B&0]'
    Im trying to devise my pattern but Im not exactly sure how to work with matching literal characters, I was lead to believe a '//' would dictate that the character is literal but this doesnt work:
    Pattern Serial = Pattern.compile("(\\/.*+\\&0])");Thanks in advance for any advice

    \ is an escape character both in Java string literals and in regex.
    "\\" produces a String containing a single \ character. But for a literal \, regex needs \\. So "\\\\" produces a single string containing \\ which in regex becomes a single literal \.
    Also, I don't think you need to escape &. And you might need to escape ] but I'm not sure--it might be okay bare if there was no preceding [.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Regex matches function

    Hi
    I am trying to come up with a regex that I can use with the matches function to validate the user id I accept. The user id can contain alphabets, numbers and 3 special chars ".","-" and "#".
    The regex I came up with was: user_id.matches("[a-zA-Z\\d\\.\\-#]"). The string I am trying to match is 'user-1'. But this fails to match.
    I am not confident about the regex I am using to match my string to. Please let me know what I am doing wrong.
    Thanks

    I would use '+' rather than '*'; I doubt that an empty string would be considered a valid user ID. ^_^   String regex = "[a-zA-Z0-9#.-]+";

  • Regex matcher class

    Hi
    I have a simple problem in regex.
    Whenever i try to write this piece of code i get an illegalStateException
    Matcher m = p.matcher(" absdsdfksj ");
    while (m.find()) {
         System.out.println("At loc : " + m.start());
         System.out.println("Found : " + m.group());
    But if i rewrite these two console print lines into one line then i dont get any exception and it runs fine:
    while (m.find()) {
         System.out.println("At loc : " + m.start() + " " + m.group());
    Pls clarify the difference.
    Thansk in advance
    Gaurav

    There must be more to the problem because I can run without problems
            Pattern p = Pattern.compile("s");
                Matcher m = p.matcher(" absdsdfksj ");
                while (m.find())
                    System.out.println("At loc : " + m.start());
                    System.out.println("Found : " + m.group());
                Matcher m = p.matcher(" absdsdfksj ");
                while (m.find())
                    System.out.println("At loc : " + m.start() + " " + m.group());
            }What pattern are you using on what data? Please give a sample of both.

Maybe you are looking for

  • Can you get a refund of $$$ on a game - not compatible with Ipod?

    I purchased a game to play on my Ipod Nano 1 but I didnt know that it would not work. Is there a way to get a refund from Itunes and get my money back? Is there a way to get a credit and use the money that I used for the game purchase towards future

  • Thumbnail images in bins

    I am developing a sequence that is comprised principally of freeze frame images taken from video clips.  I am saving all these freeze frames into a separate bin in the Browser.  I have created a 'Thumbnail' column so I can readily identify and grab t

  • Internal Muxing Error - How I Fixed Mine - FYI

    FYI: I received the "internal muxing error" message during a build. I thought I'd post what happened, what I found and how I mixed mine in case it helps someone else. I was building a project using a "project template" that I've used many many times

  • HT201263 unknown error (1015) is showing on my iphone 3 when im trying to restore settings

    unknown error (1015) is showing on my iphone 3 when im trying to restore it

  • Views in Infopath 2013

    I have created an customized list form in InfoPath 2013.I have two views. One is user information where some user data is auto populated and some are filled by the user and I have also added attachment control. And another view is manager view who re