Replace Text That Includes | Pipe Into RegEx
Moderator: DosItHelp
Replace Text That Includes | Pipe Into RegEx
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
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
Re: Replace Text That Includes | Pipe Into RegEx
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
Re: Replace Text That Includes | Pipe Into RegEx
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
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
Re: Replace Text That Includes | Pipe Into RegEx
Simply redirect the output
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:
The single command does two search and replace operations:
Here is a simpler solution that substitutes \b for every quote, but it precludes the possibility of spaceORspace within the quotes:
You must use CALL JREPL if you put either command within a batch script.
Dave Benham
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
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
Re: Replace Text That Includes | Pipe Into RegEx
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.
Re: Replace Text That Includes | Pipe Into RegEx
Thank you soooooooo much
that fixed it and I learned something too
Aisha
that fixed it and I learned something too
Aisha
Re: Replace Text That Includes | Pipe Into RegEx
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
Re: Replace Text That Includes | Pipe Into RegEx
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
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