473,698 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extracting information from String (regex, Beanshell)

11 New Member
Hello,

I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a string, separated by newline. For this i am using regex.

The String is given:

P48534
EXP value is: e-10
Q0543
EXP value is: 4e-07


My script look like this in Beanshell:

Expand|Select|Wrap|Line Numbers
  1.  import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. Pattern pGI = Pattern.compile("(^.*?$)");
  5. Pattern pEvaLue = Pattern.compile("is: (.*)$");
  6. Matcher mGI;
  7. Matcher mEvalue;
  8. StringBuffer temp = new StringBuffer();
  9. String [] line = BlastReport.split("\n");
  10. int arraysize = line.length; 
  11.  
  12. for (int i=0; i<(arraysize-1); i+=2){
  13.     String sGI = line[i];
  14.     String sEvalue = line[i+1];    
  15.     mGI = pGI.matcher(sGI);
  16.     mEvalue = pEvalue.matcher(sEvalue);
  17.     String gi="";
  18.  
  19.     if (mGI.find()){
  20.         gi =mGI.group(1);
  21.     }
  22.     if (mEvalue.find()){
  23.         String eval = mEvalue.group(1);
  24.         if(eval.startsWith("e")){
  25.             eval= "1".concat(eval);
  26.         }
  27.         Double d = new Double (eval);
  28.         double Evalue = d.doubleValue();
  29.         if (Evalue<=0.02){
  30.             temp.append(gi + "\n");
  31.         }
  32.     }
  33. }
  34.  
  35. String result = temp.toString().trim();

The error message is: "Attempt to resolve method: matcher() on undefined variable or class name: pEvalue: at Line: 16: pEvalue .matcher(sEvalu e)"


Can someone tell me why is giving me this error and how can i fix it.

Thank you in advance,

Mokita
Aug 6 '07 #1
10 11843
prometheuzz
197 Recognized Expert New Member
Hello,

I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a string, separated by newline. For this i am using regex.

The String is given:

P48534
EXP value is: e-10
Q0543
EXP value is: 4e-07

...
It looks like you're trying to capture the text after the is:, right?
Try this:
Expand|Select|Wrap|Line Numbers
  1.  String text = "P48534\nEXP value is: e-10\nQ0543\nEXP value is: 4e-07";
  2. Pattern pattern = Pattern.compile("(?<=is:\\s)[^\\n]+");
  3. Matcher matcher = pattern.matcher(text);
  4.  
  5. while(matcher.find()) {
  6.   System.out.println(matcher.group());
  7. }
Aug 6 '07 #2
r035198x
13,262 MVP
Which one is your line 16?
Aug 6 '07 #3
Mokita
11 New Member
My line 16 is:
mEvalue = pEvalue.matcher (sEvalue);

Mokita
Aug 6 '07 #4
r035198x
13,262 MVP
My line 16 is:
mEvalue = pEvalue.matcher (sEvalue);

Mokita
Java is case sensitive.
Now give yourself a kick.
Aug 6 '07 #5
prometheuzz
197 Recognized Expert New Member
My line 16 is:
mEvalue = pEvalue.matcher (sEvalue);

Mokita
That splitting of your String and increasing with 2 in your for loop looks dangerous. Perhaps I (or someone else) can suggest a better approach. But then you first need to explain what is you're trying to do.

So given the String:
Expand|Select|Wrap|Line Numbers
  1. "P48534\nEXP value is: e-10\nQ0543\nEXP value is: 4e-07"
what is it you're trying to extract and/or group?
Aug 6 '07 #6
prometheuzz
197 Recognized Expert New Member
My line 16 is:
mEvalue = pEvalue.matcher (sEvalue);

Mokita
Try this:
Expand|Select|Wrap|Line Numbers
  1. String text = "P48534\nEXP value is: e-10\nQ0543\nEXP value is: 4e-07";
  2. Pattern pattern = Pattern.compile("([A-Z]\\d+).*\\n?.*((?<=is:\\s)[^\\n]+)");
  3. Matcher matcher = pattern.matcher(text);
  4.  
  5. System.out.println(text+"\n");
  6.  
  7. while(matcher.find()) {
  8.   String id = matcher.group(1);
  9.   String sVal = matcher.group(2);
  10.   sVal = sVal.startsWith("e") ? 1+sVal : sVal;
  11.   double dVal = Double.parseDouble(sVal);
  12.   System.out.println(id+"\t"+dVal);
  13. }
Aug 6 '07 #7
Mokita
11 New Member
That splitting of your String and increasing with 2 in your for loop looks dangerous. Perhaps I (or someone else) can suggest a better approach. But then you first need to explain what is you're trying to do.

So given the String:
Expand|Select|Wrap|Line Numbers
  1. "P48534\nEXP value is: e-10\nQ0543\nEXP value is: 4e-07"
what is it you're trying to extract and/or group?

I am trying to do a workflow with taverna, which has a beanshell, where i can write a script in it.
In my workflow i will have the output of a blast search, GI number, which are the P23234 or Q12344 or A12443 or only numbers and also a E-value, which is the EXP vaule is: e-10.
From that output i want to extract the GI numbers which have an e-value<= 0.02. The way i thought i could extract was with regular expressions in java, but the way i wrote the script it is not working.

I think it is more clear what i want to do, but if you need more explanation please ask.

Mokita
Aug 6 '07 #8
r035198x
13,262 MVP
I am trying to do a workflow with taverna, which has a beanshell, where i can write a script in it.
In my workflow i will have the output of a blast search, GI number, which are the P23234 or Q12344 or A12443 or only numbers and also a E-value, which is the EXP vaule is: e-10.
From that output i want to extract the GI numbers which have an e-value<= 0.02. The way i thought i could extract was with regular expressions in java, but the way i wrote the script it is not working.

I think it is more clear what i want to do, but if you need more explanation please ask.

Mokita
I don't think regex is best for this (I could be wrong of course). You are not searching for a pattern (which is where I mostly use my regex) but you are searching for numbers within some range.
P.S I hope you managed to correct the spelling mistake for that variable.
Aug 6 '07 #9
Mokita
11 New Member
Hello

I want to thank you for your help, it is working. I also want to ask you a quick question.
How can i change group 1 ([A-Z]\\d+) to catch: P00DF3 or 1234653 or Q5647GJD or A4658DF
It is not catching the ones with letters in the midle.

Thank you again,

Mokita
Aug 6 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5185
by: Markus Ernst | last post by:
Hello I have a regex problem, spent about 7 hours on this now, but I don't find the answer in the manual and googling, though I think this must have been discussed before. I try to simply extract the title and meta tags of a valid HTML page as an array: function extract_html($filename)
3
1732
by: Stephan Bour | last post by:
I have a string ³Name² in the following format: ³LastName, FirstName (Department)² that comes from Active Directory. I need to extract the FirstName from the string. Substrings are not practical for this so I used a Regex that splits the string at the first ³, ³ and again at the ³ (³. My problem is that I don¹t know how to extract the second member of the resulting array (FirstName) and assign it to another string. Using StringBuilder, as...
1
1533
by: John Seeliger | last post by:
I am pretty new to VB, so please forgive the simplistic question. This is using VB .NET Standard 2003. My form has three objects on it: a TextBox named URL, a Button named Extract and a WebBrowser named AxWebBrowser1. The goal is to have the user enter a URL in the TextBox and then hit the Extract button and then to get the links from the web page they entered. So far I have:
1
2040
by: detho | last post by:
Hi everyone... I am currently developing a streaming movie front-end for my companies' intranet. The script is done with PHP on the back-end and HTML and CSS for the front-end. Basically, the script displays the available videos in a list of links and when the user clicks on the video, the script grabs the query string information and concatinates the appropriate filename into a variable which is used to start Windows Media Player.
3
1917
by: | last post by:
I'm analyzing large strings and finding matches using the Regex class. I want to find the context those matches are found in and to display excerpts of that context, just as a search engine might. In terms of code, what's the easiest way to make that happen? The code below works fine for identifying the matches, but it doesn't try to extract the surrounding context or display it: currPageText = numberOfMatches =...
4
13964
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
10
4074
by: Dan | last post by:
I have a number of strings that represents time. 1w 2d 3h 15m 2d 3h 15m 4h 30m 45m I want to extract the number parts of my strings into separate variables for Weeks, Days, Hours and Minutes.
11
4558
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" /> and so on. Let's suppose I want to retrieve this @url parameter, BUT ONLY with the values, in querystring, associated with "foo" and "foo2" (thus discarding name1, name2, name4 and every other different ones).
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.