Replace Text That Includes | Pipe Into RegEx

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Replace Text That Includes | Pipe Into RegEx

#1 Post by aisha » 13 Dec 2016 20:36

Hello group,
I am hoping someone can help me solve this...we have a lot of boolean TXT files such as

boolean.txt containing:
"Salad" OR "Dressing" OR "Lunch" OR ""Breakfast"

We want to convert this to RegEx in a new file called regex.txt:
\bSalad\b|\bDressing\b|\bLunch\b|\bBreakfast\b

It seems like the Pipe | is quite a problem and I have tried adding the carrot before it so ^|^ but not improvement
BatchSubstitute says it cannot work with the Pipe |

So far FART is failing for me on the Pipe |
http://fart-it.sourceforge.net/

I got closer with the SET command, so
set boo=\b
set foo=" OR "
BUT both these fail
set goo=|
set goo=^|^

so this ends up not processing the pipe:
fart -w c:\fart\boolean.txt %foo% %boo%

Any advice?

Aisha

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replace Text That Includes | Pipe Into RegEx

#2 Post by Aacini » 13 Dec 2016 21:25

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (boolean.txt) do (
   set "line=%%a"
   set "line=!line:" OR "=\b|\b!"
   echo \b!line:~1,-1!\b
)

boolean.txt:

Code: Select all

"Salad" OR "Dressing" OR "Lunch" OR "Breakfast"

output:

Code: Select all

\bSalad\b|\bDressing\b|\bLunch\b|\bBreakfast\b

Antonio

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Replace Text That Includes | Pipe Into RegEx

#3 Post by aisha » 13 Dec 2016 21:46

Antonio,
Thank you for the quick reply
This script is not changing anything or creating a new file at all.

Could there be anything wrong in the code you provided?

Aisha

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Replace Text That Includes | Pipe Into RegEx

#4 Post by dbenham » 14 Dec 2016 06:31

Simply redirect the output

Code: Select all

@echo off
setlocal EnableDelayedExpansion

>regex.txt (
  for /F "delims=" %%a in (boolean.txt) do (
     set "line=%%a"
     set "line=!line:" OR "=\b|\b!"
     echo \b!line:~1,-1!\b
  )
)

The code will not work properly if the input file contains exclamation points, has lines that begin with a semicolon, or has blank lines. There are fixes available, but I have pretty much ceased using pure batch for text file manipulation.

I should think FART could work, but I haven't tried.

I would use JREPL.BAT - a regular expression text processor. Full documentation is available from the command line via JREPL /?, or JREPL /?? for paged help.

Here is a JREPL solution:

Code: Select all

jrepl "\q([^\q]+)\q \s+OR\s+" "\\b$2\\b |" /x /t " " /f boolean.txt /o regex.txt

The single command does two search and replace operations:
  • look for a quoted string and substitute \b for the quotes
  • look for space(s) OR space(s) and replace with a pipe
You can ignore case by adding the /I switch

Here is a simpler solution that substitutes \b for every quote, but it precludes the possibility of spaceORspace within the quotes:

Code: Select all

jrepl "\q \s+OR\s+" "\\b |" /x /t " " /f boolean.txt /o regex.txt

You must use CALL JREPL if you put either command within a batch script.


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replace Text That Includes | Pipe Into RegEx

#5 Post by Squashman » 14 Dec 2016 08:08

aisha wrote:Antonio,
Thank you for the quick reply
This script is not changing anything or creating a new file at all.

Could there be anything wrong in the code you provided?

Aisha

We showed you in all of your previous questions how to redirect output to a file.

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Replace Text That Includes | Pipe Into RegEx

#6 Post by aisha » 14 Dec 2016 08:23

Thank you soooooooo much

that fixed it and I learned something too

Aisha

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replace Text That Includes | Pipe Into RegEx

#7 Post by Squashman » 14 Dec 2016 09:29

I have never used FART before. Just read the documentation.

Code: Select all

C:\BatchFiles\FART>type test.txt
"Salad" OR "Dressing" OR "Lunch" OR "Breakfast"

C:\BatchFiles\FART>fart.exe -C test.txt "\" OR \"" "\\b|\\b"
test.txt
Replaced 3 occurence(s) in 1 file(s).

C:\BatchFiles\FART>type test.txt
"Salad\b|\bDressing\b|\bLunch\b|\bBreakfast"

C:\BatchFiles\FART>fart.exe -C test.txt "\"" "\\b"
test.txt
Replaced 2 occurence(s) in 1 file(s).

C:\BatchFiles\FART>type test.txt
\bSalad\b|\bDressing\b|\bLunch\b|\bBreakfast\b

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Replace Text That Includes | Pipe Into RegEx

#8 Post by aisha » 16 Dec 2016 08:07

Squashman,
Yes, that worked very well in FART.
I am certainly not a trained programmer so when I read the documentation a lot of it makes sense but then part does not mean anything to me. So your insight helps me learn too.
thank you again

Aisha

Post Reply