1

I have an html file which has a part like this:

                <li>
                    <a href="../index.html" name="" title="title1">title1</a>
                </li>
                <li>
                    <a href="level1/level1.html" name="" title="title2">title2</a>
                </li>
                <li>
                    <a href="levl1/level2/level2.html" name="" title="title3">title3</a>
                </li>

I want them to look like this:

                <li>
                    <a href="../" name="" title="title1">title1</a>
                </li>
                <li>
                    <a href="level1/" name="" title="title2">title2</a>
                </li>
                <li>
                    <a href="level1/level2/" name="" title="title3">title3</a>
                </li>

I wrote a script in powershell to manipulate these href links which look like this:

(Get-Content $i) -replace '/*.html', '/' | Set-Content $i

But somehow the output is not coming as expected. Any idea what am I doing wrong?

1
  • How is the output coming? Is it a secret? Commented Sep 2, 2020 at 11:50

2 Answers 2

2

I would use something like this:

(Get-Content in.html) -replace '\/[a-zA-Z0-9_-]+\.html', '/' | Set-Content out.html
Sign up to request clarification or add additional context in comments.

1 Comment

because I used \w which matches any word character, it's equivalent to [a-zA-Z0-9], now I used a different match, so if you need any other strange char (but /) just add it to the [] expression
1

I would prefer to get a wider context to look for from the beginnig of the tag since the regex may find some other unexpected patterns

( Get-Content $i ) -ireplace '(<a\s+href="[^"]*/).*?(?=")','$1' | Set-Content $i

Hope that there is a typo in the 8th line otherwise you can also make 'level1/' from 'levl1/' throughout the file

( Get-Content $i ) -ireplace '(<a\s+href="[^"]*/).*?(?=")','$1' -ireplace 'levl1/','level1/' | Set-Content $i

1 Comment

Yeah its a typo. It should be level1. Thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.