473,748 Members | 2,223 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 11853
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
5190
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
1734
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
1538
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
2044
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
1930
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
13966
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
4077
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
4565
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
8983
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
8822
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
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4592
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
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.