[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

java regular expression matching on the 1st forward slash

Hi

I am trying to regexp the following in Java code

\\hello/

and NOT

\\hello/goodbye

I though i could use the regexp \\.*/ but it is incorrect.

any ideas ?

0
Molko
Asked:
Molko
  • 4
  • 3
  • 2
1 Solution
 
CEHJCommented:
Not quite sure what you mean but try
s = s.replaceAll("(^(.*?/).*$"), "$1");

Open in new window

0
 
CEHJCommented:
Sorry
s = s.replaceAll("^(.*?/).*$", "$1");

Open in new window

0
 
MolkoAuthor Commented:
Sorry, i was a bit rushed asking the question.

Looking for a RegExp that returns true on a string that is \\xxxx/ where xxx is any printable character.

I want the same regexp to return falose on a string that is \\xxxx/xxxx/

I thought \\.*/ would work, but it wont becuase / is any character and is getting matched by the .*
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MolkoAuthor Commented:
I guess something like \\[a-zA-Z0-9_]/ might be ok
0
 
CEHJCommented:
The following should do it
s.matches("\\\\[^/]+/");

Open in new window

0
 
for_yanCommented:
 String  description = "\\Hello/tt";
            String cleanDescription = description.replaceAll("\\\\.*?/","");
           System.out.println(cleanDescription);

Open in new window


output:


tt

Open in new window

0
 
for_yanCommented:
I tested this; hope this is waht you want:
      String  description = "\\Hello/tt/yyy";
            String cleanDescription = description.replaceAll("\\\\.*?/","");
           System.out.println(cleanDescription);

Open in new window


Output:
tt/yyy

Open in new window

0
 
MolkoAuthor Commented:
Nice work...

Thanks, it worked a treat
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now