ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Parsing / Filtering text

(1/2) > >>

RedPillow:
Yo again, another topic I need help with.

I have this big list in a txt-file which contains paths.

Example paths:

Motorcycle\crap\morecrap
Car\crap\morecrap
Truck\crap\morecrap
Bike\crap\morecrap

And now, I want to delete everything after the first "\" so the lines will look like this:

Motorcycle
Car
Truck
Bike

How do I do this?
What program to use?
Possibly a script?
It can`t be done with notepad`s search & replace command like this:

Search: \*
Replace: -empty-

Cause it tries to find "\*" and not "\anything".

Suggestions?

skwire:
If you're familiar with RegEx at all, here is a way to do it in AutoHotkey.  You could easily adapt the RegEx portion to your own code or a more capable editor that has RegEx search capabilities.


--- Code: AutoIt ---Text =(Motorcycle\crap\morecrapCar\crap\morecrapTruck\crap\morecrapBike\crap\morecrap) Loop, Parse, Text, `n{    If ( A_LoopField != "" )    {        RegExMatch( A_LoopField, "^(.+?)\\", SubPat )        Block .= SubPat1 . "`r`n"    }} MsgBox, % Block

ewemoa:
Here's one way to use Notepad++ for this task.

Edited the images -- should be easier for subsequent viewings :)

Open file in Notepad++
Parsing / Filtering text

Choose Search -> Replace
Parsing / Filtering text

Ensure Cursor is at Beginning of Text and Select "Regular expression" for Search Mode
Parsing / Filtering text

Fill in Appropriately Values for "Find what" and "Replace with"
Parsing / Filtering text

Click "Replace All" Button
Parsing / Filtering text

Examine the Results
Parsing / Filtering text

RedPillow:
Nice one ewemoa!!

I had to replace ¥`s with \`s thou :]

Can you break this ^([^\\]+)\\.* apart and explain what each thing there does and the \1 also?

skwire:
I had to replace ¥`s with \`s thou :]-RedPillow (February 25, 2010, 05:39 AM)
--- End quote ---

The yen symbols are actually backslashes on ewemoa's (and my) computer.  It's a side effect of using Japanese as the default language on a English Windows box.  I've become so accustomed to it over the years that my eyes don't even "see" them as yen symbols anymore.

Navigation

[0] Message Index

[#] Next page

Go to full version