Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a file hashname path generated in my tests as below. It has '/' for directory path, underscore and hyphens in random positions in file name. I want to replace this entire path using regular expression in my JPO.

What I have tried:

I tried
Java
getReplacementMap().put("regex:<[a-zA-Z0-9\\/]+[-_a-zA-Z0-9_-]+[^:]+>", "<HASHNAME>");

It is not replacing the below path and filename.

hashname: dc/c8/dcc8kixy0huy4ebs_agumhcu-qt_fcsvxn_9if3e13o.hen


Any ideas for regex?
Posted
Updated 24-Nov-23 7:41am
v2
Comments
[no name] 22-Nov-23 17:30pm    
You have a period (.) you're not accounting for? (I'm no expert)
Andre Oosthuizen 24-Nov-23 13:46pm    
Hi Gerry, the (.) is needed for the file extension.

Can you try this
getReplacementMap().put("regex:[^/]+/[a-zA-Z0-9_-]+[_-]+[a-zA-Z0-9_-]+[^.]+\\.[a-zA-Z0-9]+", "<HASHNAME>");
 
Share this answer
 
Comments
Pam04 27-Nov-23 10:50am    
This gave a very close solution. The path gets generated every time, so obviously the directories will be different.
When I tried your regex, it substituted as below...

d7/<hashname>

The first dir name was not substituted. So I tried below which worked!!

getReplacementMap().put("regex:[a-zA-Z0-9]+/[a-zA-Z0-9]+/[a-zA-Z0-9_-]+[_-]+[a-zA-Z0-9_-]+[^.]+\\.[a-zA-Z0-9]+", "<hashname>");

Thank you so much Manish!
Quote:
How do I regex a string that contains forward slases

The problem is not necessarily where you think it is.
Use tools to see what part of RegEx matches what part of string, you may find surprises.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v2
Comments
Pam04 27-Nov-23 10:55am    
Thank you so much for all the links to tools and information. I will save for future.
I had tried this tool ((https://www.freeformatter.com/regex-tester.html) before trying in my JPO. But what worked in the tool didn't work in my JPO.
Check this solution. Find a string using regex with forward slashes in it[^]
Further, you can use this link to generate and test your regular expression. RegExr: Learn, Build, & Test RegEx[^]
 
Share this answer
 
v2
Comments
Pam04 27-Nov-23 11:05am    
Thank you, will save the link for future!
Andre Oosthuizen 27-Nov-23 12:28pm    
Only a pleasure.
You can achieve your goal by simplifying it all -
Remove unnecessary angle brackets, simplify the character class for your directory and file name, still included the dot '.' in the character class for your file extension -
Java
getReplacementMap().put("regex:[a-zA-Z0-9_/]+[-_a-zA-Z0-9.-]+", "<HASHNAME>");
 
Share this answer
 
Comments
Pam04 27-Nov-23 10:52am    
This one replaced the entire test output with <hashname>
I tried below and it worked!

getReplacementMap().put("regex:[a-zA-Z0-9]+/[a-zA-Z0-9]+/[a-zA-Z0-9_-]+[_-]+[a-zA-Z0-9_-]+[^.]+\\.[a-zA-Z0-9]+", "<hashname>");

Thank you Andre.
getReplacementMap().put("regex:[a-zA-Z0-9]+/[a-zA-Z0-9]+/[a-zA-Z0-9_-]+[_-]+[a-zA-Z0-9_-]+[^.]+\\.[a-zA-Z0-9]+", "<HASHNAME>");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900