Solved

How to extract a portion of a string using regular expressions in Java

Posted on 2011-03-17
13
199 Views
Last Modified: 2012-05-11
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35157368
It looks like all that's happening is that you're appending

mycheck/report.html

..?
0
 

Author Comment

by:Tolgar
ID: 35157462
Yes. But in some cases I remove some portion of it like "sbtest/mylog_results.html" and in some other cases I don't need to remove.

So it is not actually just adding "mycheck/report.html" to the end of each link.

Hope this explains better

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35157592
You'd have to define the rules, otherwise  it's impossible to say
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tolgar
ID: 35157882
You are right. Here is the rule:

This part is fixed:

http://www-internal.mywork.com/mywork/devel/sandbox/ANYTEXT/

The rest is not clear. But let me try to make a rule.

Apqr_test_mytests/glnxa64
mytests_logs/02-23-2011/glnxa64
prequal_testlog/Apqr_j123456_mytests/glnxa64/sbtest/mylog_results.html
prequal_testlog/Apqr_j123456_mytests/glnxa64/sbtest/mylog_results.html
MERGE_mytests/glnxa64_2011-02_sidCopy


From these example cases the rule is:
Note: Parathesis can be ignored. They are not part of the rule. I just used them to group parts.

http://www-internal.mywork.com/mywork/devel/sandbox/ANYTEXT/ ( 1 or many ANYTEXT/ ) ( 1 or 0 ANYTEXT.html )


Thanks,
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35158124


Still, not quite clear to me what is the rule:

I see, you start from:

http://www-internal.mywork.com/mywork/devel/sandbox/ANYTEXT/ ( 1 or many ANYTEXT/ ) ( 1 or 0 ANYTEXT.html )



You are adding mycheck/report.html
 but you are removing some part, where, I guess, there is already report.

But how would one know, say,  that "/sbtest/" should also be removed?


0
 

Author Comment

by:Tolgar
ID: 35158275
You are right.

The rule that I wrote is for the match only. Now from this match I want to remove the part to the beginning of sbtest if there is any sbtest. And add mycheck/report.html

If there is no sbtest then directly add mycheck/report.html.

And from the rule I have given before, we don't know how many directories we have (ANYTEXT/) until the x.html file.

I hope it is clear now.

Thanks,
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35158334
The following should be OK then
int ix = 0;
if((ix =s.indexOf("/sbtest")) > -1) {
	s = s.substring(0, ix);
}
s += suffix;

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35158335


Don't know about regular expressions, but this is how you can do it with regual java:

String s1 = s.substring(0, s.lastIndexOf("/"));
if(s1.endsWith("sbtest"))s1 = s1.substring(0,s1.lastIndexOf("/"));
s1 += "/mycheck/report.html";
0
 

Author Comment

by:Tolgar
ID: 35158580
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35158597
'suffix' in my example is of course

"/mycheck/report.html"
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35158656
Yes, you are right; CEHJ's is more general, I assumed that sbtest is always the last folder which contains the .html file
0
 

Author Comment

by:Tolgar
ID: 35158765
Thanks guys...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35158839
:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question