Simple Java regex question

I have a file with set of Name:Value pairs
e.g
Action1:fail
Action2:pass
Action3:fred
Using regex package I Want to get value of Name "Action1"
I have tried diff things but I cannot figure out how I can do it. I can find Action1: is present or not but dont know how I can get value associated with it.
I have tried:
Pattern pattern = Pattern.compile("Action1");
CharSequence charSequence = CharSequenceFromFile(fileName); // method retuning charsq from a file
Matcher matcher = pattern.matcher(charSequence);
if(matcher.find()){
     int start = matcher.end(0);
     System.out.println("matcher.group(0)"+ matcher.group(0));
how I can get value associated with specific tag?
thanks
anmol

read the data from the text file on a line basis and you can do:
String line //get this somehow
String[] keyPair = line.split(":")g
System.out.println(keyPair[0]); //your name
System.out.println(keyPair[1]); //your valueor if you've got the text file in one big string:
String pattern = "(\\a*):(\\a*)$"; //{alpha}:{alpha}newline //?
//then
//do some things with match objects
//look in the API at java.util.regex

Similar Messages

  • Simple Java Coding Question

    I know this is a simple question, but I can't search for the answer to this because it involves syntax that Google and the like don't seem to search for..
    I am trying to figure out some Java code that I have found. I would like to know what:
    a[--i] and a[++j] mean.
    My first thought was that they meant to look at the position in the array one less (or more) than i (j). But that doesn't seem to be the case. A is an array of Strings, if that matters...
    Thanks for you help.

    muiajc wrote:
    I know this is a simple question, but I can't search for the answer to this because it involves syntax that Google and the like don't seem to search for..
    I am trying to figure out some Java code that I have found. I would like to know what:
    a[--i] and a[++j] mean.
    It mean increase/decrease the int i/j by 1. This is done before it gets you the value of a[]. for example if i=5 and you said a[--i] it is really a[4] and i is now equal to 4;

  • Simple Java EJB question.

    When it comes to EJB v3 Remote and Local interfaces,
    (With JBoss EJB Container Software in mind, for this question,)
    -Does the Remote interface for the EJB include method signatures from the Bean class
    -that are to be seen
    -that are to be hidden?
    -Does the Local interface for the EJB include method signatures from the Bean class
    -that are to be seen
    -that are to be hidden?
    Which is which for
    -EJB 2.1 ?
    -EJB 3.x ?
    - is EJB 3.x reverse compatible in that it allows use of the javax.ejb.* interfaces,
    and would accept a EJB 2.1 approach, or does it force one to use Annotations
    exclusively?
    Edited by: Zac1234 on Jul 21, 2010 5:21 PM

    muiajc wrote:
    I know this is a simple question, but I can't search for the answer to this because it involves syntax that Google and the like don't seem to search for..
    I am trying to figure out some Java code that I have found. I would like to know what:
    a[--i] and a[++j] mean.
    It mean increase/decrease the int i/j by 1. This is done before it gets you the value of a[]. for example if i=5 and you said a[--i] it is really a[4] and i is now equal to 4;

  • Plzzz write prog to this simple java threads question

    Write a program in C to create four processes. The original processes creates
    two children before it prints out "PARENT". The children processes print
    "CHILD1" and "CHILD2" respectively. The first child process creates a child
    process that prints out "GRANDCHILD".
    Note: The output must be guaranteed to print out in the following order each
    time it is executed:
    GRANDCHILD
    CHILD2
    CHILD1
    PARENT
    a. Write the program using C or C++ and forks to create the children processes.
    b. Write a second program using Java and Threads for the children processes.
    The output statements for CHILD1, CHILD2 and GRANDCHILD must come from print
    statements in the run method of the thread.

    Most people here will not do your homework for you. If you post a reasonable question that shows you've made some effort, and what the results of that effort were, and provides details about what parts you're having trouble with, then somebody will probably provide guidance so that [you can complete the work.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Simple Java SDK question for server group assignment

    I have a snippet of code below in which I am trying to apply the setProcessingServerGroup() and setProcessingServerGroupChoice() methods to my report object. My question should be relatively simple: I believe I need to cast(?) my iObject report object to iProcessingServerGroupInfo per the API in order to use the setProcessingServerGroup() and setProcessingServerGroupChoice() methods, but I'm not sure how to do so.
    Can someone please advise how to cast my iObject to iProcessingServerGroupInfo?
    Thanks in advance...
    Code:
    IInfoObject iObject = (IInfoObject) childReports.get(i); 
    int sgID_view = Integer.parseInt(serverGroupID_view);
    int sgPref_view = Integer.parseInt(serverGroupPref_view);
    !!!!!  CONVERSION / CAST NEEDED HERE !!!!!
    iProcessingServerGroupInfo.setProcessingServerGroup(sgID_view);
    iProcessingServerGroupInfo.setProcessingServerGroupChoice(sgPref_view);

    To followup, I've been able to cast to IShedulingInfo in order to use the setServerGroup() and setServerGroupChoice() methods successfully:
    IInfoObject iObject = (IInfoObject) iObjects.get(i);    
    ISchedulingInfo iSchedulingInfo = iObject.getSchedulingInfo();
    iSchedulingInfo.setServerGroup(427);
    iSchedulingInfo.setServerGroupChoice(2);
    But I don't know how to perform the cast to be able to access the setProcessingServerGroup(), sterProcessingServerGroupChoice() methods.
    Any help appreciated.
    Thanks!

  • Simple java architecture question

    This should be a simple question to answer but I haven't been able to find a good answer.
    I have an application that establishes a network connection with a server and registers event listeners on that connection.
    What I want to do is find a way without a busy-wait/while loop to keep the main() thread running so that the listeners can do their job.
    Here is a sample of what I've got:
    public static void main(String[] args)
    SomeConnection conn1 = null;
    try
    conn1 = new SomeConnection("someaddress");
    TrafficManager tm = conn1.getTraffictManager();
    TrafficHandler th = tm.createTraffichandler(new MessageListener()
                        public void processMessage(Message message)
                             System.out.println("Received: " + message.toString());
         catch (Exception e)
              e.printStackTrace(System.out);
         conn1.disconnect();
    The problem is that the application doesn't stay running to respond to traffic coming across the connection.
    Any guidance would be appreciated.
    Thanks

    Well, what is the job of the MessageListener if it isn't to listen for messages? And apparently it isn't doing that because your application terminates.
    Bear in mind that I don't have any idea how any of those four classes work, or even how they are supposed to work. So let me just quote this line from the API documentation of the Thread class which says when your application will terminate:
    "All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method."
    That must be the case in your application.

  • Simple Java Sound Question...

    Can I draw waveforms while capturing data? If yes, how. If no, why.
    Thanks. I need the answer ASAP, please help.

    Hi nfactorial,
    Can I draw waveforms while capturing data?If you would like to do that in Java you would need to know about how to use the Java language in general and especially about the Java Sound and Java2D APIs.
    If yes, how.It would be too much to explain to answer such a general question. A general answer is, you need to use the Java Sound API to capture the data for which a waveform should be drawn. The Sound API delivers data as a stream of bytes typically encoded in pulse coded modulation (PCM) format. The data stream has digital samples each representing a level of sound pressure for a certain point in time. The stream of samples in the amplitude/time domain need to be transformed to a spectrum of samples in the amplitude/frequency domain, i.e. a number of PCM samples need to be mapped to the frequencies that should be displayed. This is done with the fast fourier transformation algorithm. Each set of amplitude/frequency values can then be displayed as a waveform by using some line drawing logic. The entire process would need to run constantly, i.e. as bytes are received from the sound data stream transformation and drawing is triggered.
    Related readings:
    Java Tutorial
    http://java.sun.com/docs/books/tutorial/
    Java Sound Documentation
    http://java.sun.com/j2se/1.5.0/docs/guide/sound/index.html
    Java Sound API Programmer's Guide
    http://java.sun.com/j2se/1.5.0/docs/guide/sound/programmer_guide/contents.html
    Java Sound Resources
    http.//www.jsresources.org
    Java 2D Graphics Tutorial
    http://java.sun.com/docs/books/tutorial/2d/index.html
    Wikipedia on fast fourier transformation
    http://en.wikipedia.org/wiki/Fast_fourier_transform
    HTH
    Ulrich

  • Simple Java Array Question

    // This is how I was taught to make a simple array. You have to define the size first.
    String [] test1 = new String[4];
    test1[0] = "1";
    test1[1] = "2";
    test1[2] = "3";
    test1[3] = "4";
    out.println(test1[0]);
    out.println(test1[1]);
    out.println(test1[2]);
    out.println(test1[3]);
    //If the size is not defined in this second example, why does it still work?
    String [] test2 = {"1","2","3","4"};
    out.println(test2[0]);
    out.println(test2[1]);
    out.println(test2[2]);
    out.println(test2[3]);
    They both produce the same results.

    // This is how I was taught to make a simple array.
    You have to define the size first.
    String [] test1 = new String[4];
    test1[0] = "1";
    test1[1] = "2";
    test1[2] = "3";
    test1[3] = "4";
    out.println(test1[0]);
    out.println(test1[1]);
    out.println(test1[2]);
    out.println(test1[3]);
    //If the size is not defined in this second example,
    why does it still work?
    String [] test2 = {"1","2","3","4"};
    out.println(test2[0]);
    out.println(test2[1]);
    out.println(test2[2]);
    out.println(test2[3]);
    They both produce the same results.Umm..the size is just as well defined in your second as in your first...it is a four element array.
    The second way is akin to the first...also
    String[] s = new String[]{"Hello","I","contain","five","elements"};
    System.out.println(s[5]); Will compile..you will get a run-time ArrayIndexOutOfBoundsException (it is a 5 element array, 0-4, but java will not assume that the variable s is not changed between those two lines of code)..
    Array index/boundary checking is done at run-time, not compile-time.
    In fact:
    String[] s;
    //let's say up in the program
    //and
    public void printS(int i)
    System.out.println(s);
    Will also compile...and if something else happens to set s as a 10 element String array:
    printS(9);would work
    ~David

  • Simple java pgm - question

    Hallo,
    to the following pgm I had question:
    static void printPrimes (int max) (= wavy bracket;
    boolean(should be angular brackets) sieve = new booolean (angular br max+1
    angular br)
    int i,j;
    for (i=2; i = max;i++)sieve(angluarbr i angular br)= true
    i= 2
    while (i= <= max;) (=wavy bracket Out.println (i+ " "); // is prime
    for (j=i; j <= j+i) sieve(angular br j angularb.) = false;
    while (i <= maxAmpersandAmpersand !sieve (angular br angular br)I++;
    ) wavy br ) wavy br.
    I hope this is still kind of readable.
    Q's are:
    after the first "for" there is a new i=2, which seems to staty at 2 all the time; the following while does not change this value ( the beginning for i++ does not change this) and so seems to print 2 at all times (??)
    then the inner loop seems to count up all the doubbles of the new prime number and thrwoes it out as false at the end of it;
    the i values (increased with i++ seemsto refere at all times to the unit numbers and only indirect to the un it values, which are written/ assigned to it (??)
    I seem to wonder how the pgm can print out anythings else the 2's.
    I would appreciate any help to that.
    ml
    griz

    I hope this is still kind of readable.No, it's not. http://forum.java.sun.com/help.jspa?sec=formatting
    Why are you omitting the braces and brackets? And why don't you type "program"? Aren't we worth the additional time it takes?
    Q's are:
    after the first "for" there is a new i=2, which
    seems to staty at 2 all the time; the following
    while does not change this value Why should it? You don't tell it to.
    ( the beginning for
    i++ does not change this)It does. It just gets re-set to 2 after the for loop-
    and so seems to print 2 at
    all times (??)Yeah. What else?
    then the inner loop seems to count up all the
    doubbles of the new prime number and thrwoes it out
    as false at the end of it; That part is definitely unreadable.

  • Java Regex Question (HTML Tokenizing

    Hello
    I would like to tokenize a HTML Page into its html tags and could not find any working expression. I tried it with:
    <[.]*>
    and for all input fields:
    <(INPUT.*)>
    But it doesn't find anything either or it findes anything.
    Can somebody help me?

    </?\S+?[\s\S+]*?>
    "/?" means: "/" can be there but doesnt have to
    "\S" means: every character which isnt a whitespace
    "+" means: look for the previous character if it is there at least one time.
    the "?" after the "+" means: look only for as few of the previous characters as needed to fullfill the regex.
    thats why <adf>sdf> isnt found because <adf> is the shortest string that fullfills the regex.
    "[]" means: treat everything inside the brackets as one term
    "\s" means: look for a whitespace
    "*" means: the previous character (which is the term inside the brackets) can be there as many times as it wants, even zero times
    "*?" is like "+?"

  • Java Regex Question

    I wanted to do some regex to see if a string has a subdomain.
    I want to pass string then check if there is a xxx.example.com or if it's just example.com. Anyone have a clue?
    Thanks,
    Brian

    I just went around and used the split method to check, I'm posting my code in case someone else has this problem and limited to the 1.4 jdk.
    String split = domain.split("[.]") ;
    if(split.length > 2)
        domain = split[split.length - 2] + "." + split[split.lengh -1] ;basically what I wanted to do was see if it was a subdomain and then strip the preceding and just get to the actual domain.
    Thanks for the replys

  • Java Regex Question extract Substring

    Hello
    I've readt the regex course on http://www.regenechsen.de/regex_de/regex_1_de.html but the regex rules described in the course and its behavior in the "real world" doesn't makes sense. For sample: in the whole string: <INPUT TYPE="Text" name="Input_Vorname">
    the matcher should extract only the fieldname so "Input_Vorname" i tried a lot of patterns so this:
    "name="(.*?)\"";
    "<.*name=\"(.*)\".?>";
    "<.*?name=\"(w+)\".*>";
    "name=\".*\"";
    and so on. But nothing (NOTHING) works. Either it finds anything or nothing. Whats wrong ?
    Can somebody declare me what I've made wrong and where my train of thought was gone wrong?
    Roland

    When you use the matches() method, the regex has to match the entire input, but if you use find(), the Matcher will search for a substring that matches the regex. Here's how you would use it:  String nameRegex = "name=\"(.*?)\"";
      Pattern namePattern = Pattern.compile(nameRegex,Pattern.CASE_INSENSITIVE);
      Matcher nameMatcher = namePattern.matcher(token);
      if (nameMatcher.find()) {
        String fieldName = nameMatcher.group(1);
      }But the main issue is that you're using the wrong method(s) to retrieve the name. The start() and end() methods return the start and end positions of the entire match, but you're only interested in whatever was matched by the subexpression inside the parentheses (round brackets). That's called a capturing group, and groups are numbered according to the order in which they appear, so you should be using start(1) and end(1) instead of start() and end(). Or you can just use group(1), as I've done here, which returns the same thing as your substring() call.
    Knowing that, you could go ahead and use matches(), with an appropriate regex:  String nameRegex = "<.*?name=\"(\\w+)\".*?>";
      Pattern namePattern = Pattern.compile(nameRegex,Pattern.CASE_INSENSITIVE);
      Matcher nameMatcher = namePattern.matcher(token);
      if (nameMatcher.matches()) {
        String fieldName = nameMatcher.group(1);
      }

  • Problem with getNodeValue() - simple Java XPath question

    I am trying to extract the node values from a XML file:
    <root>
      <frame>
         <boxes>
           <box>
              <spec>22</spec>
              <spec>2222</spec>
           </box>
           <box>
              <spec id="BA" value="short"/>
              <spec id="BB" value="thin"/>
              <spec id="BC" value="black"/>
              <spec id="BD" value="full"/>
              <spec id="BE" value="7"/>
              <spec id="BF" value="deactive"/>
           </box>                
         </boxes>
         <circles>
           <circle id="CA" value="blue"/>
           <circle id="CB" value="green"/>
         </circles>
      </frame>
    </root>I use this code:
             XPathFactory factory = XPathFactory.newInstance();
             XPath xpath = factory.newXPath();
             XPathExpression xPathExpression = xpath.compile("/root/frame/boxes/box/spec");            
             Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);
             NodeList nodes = (NodeList) result;
             for (int i = 0; i < nodes.getLength(); i++) {
                 System.out.println(nodes.item(i).getNodeValue());
             }But only:
    null
    null
    null
    null
    null
    null
    null
    null
    get printed. I would have assumed:
    22
    2222
    null
    null
    null
    null
    null
    null
    since the two first spec nodes contains the above numbers. Any ideas?

    change this:
    XPathExpression xPathExpression = xpath.compile("/root/frame/boxes/box/spec");to this:
    XPathExpression xPathExpression = xpath.compile("/root/frame/boxes/box/spec/text()");Need to xpath all the way down to the text of the node.
    You could use
    System.out.println(nodes.item(i).getTextContent());with your current xpath but if the spec node has children, these will also be included in the output.

  • Efficiency Question: Simple Java DC vs Used WD Component

    Hello guys,
    The scenario is - I want to call a function that returns a FileInputStream object so I can process it on my WD view. I can think of two approaches - one is to simply write a simple Java DC that does so and just use it as a used DC so I can call the functionality from there. Or create another WD DC that exposes the value (as binary) via component interface.
    I'm leaning on the simple Java DC approach - its easier to create. But I'm just curious on what would be the Pro-side (if there's any) if I use the WD Component interface approach? Is there a plus for the efficiency? (Though I doubt) Or is it just a 'best/recommended practice' approach?
    Thanks!
    Regards,
    Jan

    Hi Jan
    >one is to simply write a simple Java DC that does so and just use it as a used DC so I can call the functionality from there
    Implemented Java  API for the purpose mentioned in your question is the right way, I think. The Java API can be even located in the same WebDynpro DC as your WebDynpro components. There is not strongly necessity to put it into separate Java DC.
    >Or create another WD DC that exposes the value (as binary) via component interface
    You should not do this because in general WD components' purpose is UI, not API. Implementing WD component without any UI, but with some component interface has very-very restricted use cases. Such WD component shall only be a choice in the cases when the API is WebDynpro based and if you have to strongly use WebDynpro Runtime API in order to implement your own API.
    If your API does not require WebDynpro Runtime API invocations or anything else related to WebDynpro then your choice is simple Java API.
    >But I'm just curious on what would be the Pro-side (if there's any) if I use the WD Component interface approach? Is there a plus for the efficiency?
    No. Performance will be better in simple Java API then in WebDynpro component interface. The reason is the API will not pass thought heavy WebDynpro engine.
    BR, Siarhei

  • Converting sed regex to Java regex

    I am new to reguler expressions.I have to write a regex which will do some replacements
    If the input string is something like test:[email protected];value=abcd
    when I apply the regex on it,this string should be changed to test:[email protected];value=replacedABC
    I am trying to replace test.com and abcd with the values i have supplied...
    The regex I have come up with is in sed
    s/\(^.*@\)\(.*\)$/\1replaceTest.com;value=replacedABC/i
    Now I am trying to get the regex in Java,I would think it will be something like (^.*@\)(.*\)$\1replaceTest.com;value=replacedABC
    But not sure How i can test this.Any idea on how to make sure my java regex is valid and does the required replacements?

    rsv-us wrote:
    Yep.Agreed.
    Since that these replacements should be done in a single regex.Note that the sed replacement I posted is really made of two replacements! Just like your Java solution would.
    I think once we send this regex to the third party,they will haev to use either sed or perl(will perl do this replacements,not sure though) to get the output.
    Since we are not sure what tool/software the third party is going to use,I was trying to see how i can really test this.Then I read about sed and this regex as is didn't work,so,I had to put all the sed required / and then the regex had become like s/\(^.*@\)\(.*\)$"/1replaceTest.com;value=replacedabcd/iAgain: AFAIK that does not work. I tried it like this:
    {code}$ echo test:[email protected];value=abcd | sed 's/\(^.*@\)\(.*\)$"/1replaceTest.com;value=replacedabcd/i'and the following is returned:test:[email protected] that we will have to send the java regex to the third party,I was trying to see how i can convert this sed regex to java.If I am right,with jave regex,we won;t be able to all the finds and replacements in a single regex..right?...If this is true,this will leave me a question of whether I need to send the sed regex to the thrid party or If I send java regex,they have to convert that to either sed or perl regex.
    One more question,can we do thse replacement in perrl also,if so,what will the equivalent regex for this in perl?
    I can't understand what you are talking about. The large amount of spelling errors also doesn't help to make it clearer.
    Good luck though.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Maybe you are looking for

  • Populating  the segments of idocs

    Hi,     How do segments of the idoc get populated with data from SAP tables ?  (for SAP standard idoc or newly created idoc and outbound process) Can anyone say in details? Good answer will be rewarded Thanks , Manas Behera

  • Text messages different than what I sent

    Recently I've been noticing that my text messages that I'm sending to my friend aren't what I'm actually sending. Part of the texts are what I said but then it completely changes everything and actually ends up sending something I sent weeks/months a

  • I just need an e-mail address!

    Where do I find an e-mail address to e-mail Apple Corporate? I've been looking and cannot find where to go. I am sorry to post this in here but I do not know where else to ask. If anyone replies can you PLEASE post a link? Or at least list exactly wh

  • Why does my new iMac keep freezing for no apparent reason?

    Why does my new iMac keep freezing? This usually happens when it is not being used and is particularly frustrating when I do need to use it and ahve to switch off and then on again and wait for it to reboot. I am not computer literate so keep any ans

  • Determining PST time from SYSTIMESTAMP

    Thanks to everyone who answered by previous question about SYSTIMESTAMP. Here's a PL/SQL function I wrote to convert the current time to PST and return it as a DATE value. The idea is that it should work regardless of what the database's timezone is