• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex expressions

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to get only 7212 from a string like this one

I have tried with the expression

^\A{;}/z$

I try to express it should get after the first

;

till the second

;



I am using https://regex101.com/ it gives some advice about the errors and still I cant solve completely the expression


Any idea, please?

Regards, Isaac
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use a Scanner with useDelimiter(";")? Perform next() twice and you're done.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner is almost *always* a better solution than applying regular expressions directly. Take the time to get intimate with its API, because dissecting strings will become much easier.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just to be a bastard: "Regex expression" is a tautology ;)
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you want to refer to that site assuming that you are writing code in java, pleas refer java's regex documentation.
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
I am still unsure about how this can be solved using regex.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Salvin, it can be done using capturing groups. Describe the part of the input you want to capture, and then either see if the input matches(), or find() the next occurrence. Then return the value of the capturing group().

However, Scanner is more elegant.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the regex itself ...

Isaac Ferguson wrote:I have tried with the expression

^\A{;}/z$


What are you trying to do here? This is not even close to a valid regex. If it was close, we could probably figure out what you are trying to accomplish, and suggest a fix -- but this expression is (with the exception of the first and last character) no where close to a valid regex.

Henry
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would definitely recommend Scanner.

But I just wanted to check with regex



and then do a substring from matcher1.end() to matcher2.end()-1 if both the finds are true ?

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:
and then do a substring from matcher1.end() to matcher2.end()-1 if both the finds are true ?



You do know that you used the regular expression engine to do ... essentially an "index of" for the first two semicolon characters right? Isn't it much easier to just use the indexOf() method instead?

Henry
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
You do know that you used the regular expression engine to do ... essentially an "index of" for the first two semicolon characters right


Yes

Henry Wong wrote:
Isn't it much easier to just use the indexOf()


I agree

Stephan van Hulst wrote:Salvin, it can be done using capturing groups. Describe the part of the input you want to capture, and then either see if the input matches(), or find() the next occurrence. Then return the value of the capturing group().


I was just trying Stephan's proposal actually to see if I was in line with what he meant.

I too said a Scanner is a better choice. I would have simply used a StringTokenizer
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

Stephan van Hulst wrote:Salvin, it can be done using capturing groups. Describe the part of the input you want to capture, and then either see if the input matches(), or find() the next occurrence. Then return the value of the capturing group().


I was just trying Stephan's proposal actually to see if I was in line with what he meant.



Stephan was talking about using capturing groups to capture the result. While you have capturing groups in your regex, you are certainly not using them. Instead you are obtaining the result via the substring() call -- and not via capturing group.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The regex is also concerned with too much information. Why do you need to describe the part that comes before the first semi-colon? You don't care what it looks like, so don't describe it. Use either Matcher.find() with the pattern ";(\\d+);", or use Matcher.matches() with the pattern ".*?;(\\d+);.*".
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I too said a Scanner is a better choice. I would have simply used a StringTokenizer


Java™ Platform, Standard Edition 8 API Specification wrote:StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info guys, its news to me that string tokenizer is discouraged maybe am not so updated after all.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have tried with Scanner like this:



I got from the info web Scanner API

What I need is get from a string like this


is only the second element in this case 7212

When I debug

sc.next();

it gives me other parts of the string like "HTTP Request" or others depending of the line it is reading from the textFile instead of stay getting every second element corresponding to miliseconds of every line.

Any idea, please?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, we can't really help you if we don't know what the inputs are that you're using, what you're expecting to see, and what the actual outputs are.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:
it gives me other parts of the string like "HTTP Request"



It will help us help you better if you posted 2-3 lines within that file that your program gives erroneous readings on.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using new file from a new JMeter test, it looks like this:



The code i am debugging looks like this:



In the first iteration I get "12:41:43" and "HTTP sydney" it doesnt gets the value I am looking for "287" and in the second iteration It gets "True" and "13"

Any idea, please?

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I run that exact piece of code against the data you provided, I get precisely the results you want.

timeStamp
elapsed
12:41:43
287
12:41:43
495
12:41:43
672
12:41:43
676

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are trying to extract time elapsed through JMeter's log.

Although, this is unrelated to your actual post, but have you tried adding a listener like 'View Results in Table' and then save that as a csv ?
You could open that csv directly in Excel and get all the data you need.

If you are trying to calculate something on the time elapsed such as average, median, 90% line, 99% line, Min, Max You could use the 'Aggregate Report' listener.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok,

then something else is missing in my code because I get this result when printing it I think I got confused when debugging:

Number of requests at labelHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP sydneyHTTP

I have a small Servlet which calls a class.




Any idea, please?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well... yeah. You're appending the third token of each line to msn. The third token of each line is "HTTP sydney". It can only do this if it correctly reads "12:41:43" as the first token, and "287" as the second token. There is no error, I think you're confused.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why i need two

If I write this code and I return it to the Servlet I get

sydneyHTTP



Any idea, please?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove one of the two sc.next() calls?
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then if I remove one it shows the first one

18:53:10

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a code repository where you could publish an SSCCE showing your problem?
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can send you a link to GIT with only the basic code

Does it helps?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can run your code, but obviously I can't do much without having access to the data you're using. I don't have "test-resultSidney.csv".
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I cant attach .page documents I ned you some rows just for run it.



Does it helps?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I can't reproduce your problem, so unless you come up with an SSCCE for us, there's not much we can do for you.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it not what I sent to you? A repository with the minimal code?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. An SSCCE in this case would be a simple class in which the main method calls the method that attempts to read the values that you want from a big String that represents your text file and prints them to console.

For it to be an SSCCE, all we should have to do is paste your code in our IDE, and just hit the run button.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:Is it not what I sent to you? A repository with the minimal code?


We really want to help you with your problem, but we aren't able to reproduce your issue at our end, hence we are unable to help you.
Here is my version of an SSCCE for your code with Stephan van Hulst's solution:



You are free to play with testLines[] array and add your own values. If you get the "Error: couldnt extract time taken for line ..." statement, maybe you can post that line here
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made work that piece of code, and now I m integrating it into the MVC app with Servlets. It compiles etc, and it doesnt throws errors. For this line of code it doesnt returns any value.



when debugging I get a string in the return statement.

Any idea,, please?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you printing html?

What value are you expecting to see? What value does it return? TellTheDetails
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to get back a String from the call to the method
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it works the code in the Servlet is like this:



Anyone know how to apply CSS styles to a Servlet in order to render it in the index.jsp?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which method? Which return statement?

Again, please post an SSCCE, and describe what output you're expecting when you run it as-is.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It already works well the line

analyzerReader.conector(file_name, loadUsers, loadTime)

returns a String properly. All is working now what I interestd is in apply CSS to that prints .out
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be using a framework that handles output for you. MVC frameworks allow you to return a view that is built up using templates. You can then just refer to your CSS from your template.

Do you know about RESTful web services and MVC?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic