1638: "Backslashes"

This forum is for the individual discussion thread that goes with each new comic.

Moderators: Moderators General, Prelates, Magistrates

User avatar
Eternal Density
Posts: 5579
Joined: Thu Oct 02, 2008 12:37 am UTC
Contact:

1638: "Backslashes"

Postby Eternal Density » Wed Feb 03, 2016 5:45 am UTC

Image
Title text: I searched my .bash_history for the line with the highest ratio of special characters to regular alphanumeric characters, and the winner was: cat out.txt | grep -o "\\\[[(].*\\\[\])][^)\]]*$" ... I have no memory of this and no idea what I was trying to do, but I sure hope it worked.


I'd like to know how Randall did that search. Possibly with some sort of regular expression containing a lot of special characters.

Also, https://regex101.com/ tells me that expression has unbalanced parentheses, because it doesn't know that \\\ is a real real backslash, so it mistakenly escapes the [ as a real square bracket instead of a list of characters.
Last edited by Eternal Density on Wed Feb 03, 2016 5:53 am UTC, edited 1 time in total.
Play the game of Time! castle.chirpingmustard.com Hotdog Vending Supplier But what is this?
In the Marvel vs. DC film-making war, we're all winners.

Quey
Posts: 29
Joined: Wed Sep 24, 2014 12:05 am UTC

Re: 1638: "Backslashes"

Postby Quey » Wed Feb 03, 2016 5:48 am UTC

Code: Select all

cat out.txt |

Looks like someone needs a little demoggification? But who am I kidding? I do the same thing fairly often.

savageorange
Posts: 16
Joined: Wed Aug 04, 2010 8:03 am UTC

Re: 1638: "Backslashes"

Postby savageorange » Wed Feb 03, 2016 5:50 am UTC

Using single quotes instead of double tends to minimize that kind of madness. '\\[' == "\\\["

Quey wrote:

Code: Select all

cat out.txt |

Looks like someone needs a little demoggification?

It's doubly redundant since grep actually accepts a list of input files..
Last edited by savageorange on Wed Feb 03, 2016 5:53 am UTC, edited 1 time in total.

ps.02
Posts: 378
Joined: Fri Apr 05, 2013 8:02 pm UTC

Re: 1638: "Backslashes"

Postby ps.02 » Wed Feb 03, 2016 5:53 am UTC

Quey wrote:

Code: Select all

cat out.txt |

Looks like someone needs a little demoggification?

I hadn't heard it called that - in comp.unix.shell Randal Schwartz used to give out UUOC (Useless Use of Cat) awards, so that's the term I think of.

But hey! At least Randall M. didn't call "/" a backslash, as some are wont to do.

User avatar
rhomboidal
Posts: 797
Joined: Wed Jun 15, 2011 5:25 pm UTC
Contact:

Re: 1638: "Backslashes"

Postby rhomboidal » Wed Feb 03, 2016 6:02 am UTC

How highly irregular.

User avatar
Eternal Density
Posts: 5579
Joined: Thu Oct 02, 2008 12:37 am UTC
Contact:

Re: 1638: "Backslashes"

Postby Eternal Density » Wed Feb 03, 2016 6:05 am UTC

Breakdown of \\\[[(].*\\\[\])][^)\]]*$
(thanks to https://regex101.com/ )

\\\ matches the character real real backslash literally

[[(] match a single character present in the list below
[( a single character in the list [( literally

.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]

\\\ matches the character real real backslash literally

[\])] match a single character present in the list below
\] matches the character ] literally
) the literal character )

[^)\]]* match a single character not present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
) the literal character )
\] matches the character ] literally

$
assert position at end of the string
Play the game of Time! castle.chirpingmustard.com Hotdog Vending Supplier But what is this?
In the Marvel vs. DC film-making war, we're all winners.

iabervon
Posts: 61
Joined: Fri Nov 03, 2006 5:25 am UTC

Re: 1638: "Backslashes"

Postby iabervon » Wed Feb 03, 2016 6:18 am UTC

Find and print: a backslash, then an open bracket or open parenthesis, then anything, then a backslash, then a close bracket or close parenthesis, then anything that's not close brackets or close parentheses, at the end of the line.

I'm leaning toward "switched to a different method after that was still not quite right".

User avatar
wraith
Posts: 101
Joined: Mon Aug 06, 2007 6:58 am UTC
Contact:

Re: 1638: "Backslashes"

Postby wraith » Wed Feb 03, 2016 6:43 am UTC

You should try CMake macros. They resolve backslashes every time. So if you have a macro, calling a macro, calling a macro, needing a backslash you need to call:

Code: Select all

macro("\\\\\\\\")


... then you start writing scripts which generate power-of-two backslashes, based on the call stack depth, until you finally just start using you own special character instead of backslash (say "~"), and just replacing it with a mere 4 at the very deepest macros (ah, yes, 4, because they'll become two in your replace function, and then one - the actual one you needed - in you actual call)

The true name of Ba'al is CMake.

PS: Actual line of code from before we started replacing backslashes with tildes:

Code: Select all

source_group_from_dir("Platform\\\\\\\\\\\\\\\\plugins\\\\\\\\\\\\\\\\SplashScreen" platform_sources "${APP_ROOT_DIR}/${PLATFORM_DIR}/plugins/SplashScreen")
Last edited by wraith on Wed Feb 03, 2016 6:46 am UTC, edited 1 time in total.
It's only when you look at an ant through a magnifying glass on a sunny day that you realize how often they burst into flames

ps.02
Posts: 378
Joined: Fri Apr 05, 2013 8:02 pm UTC

Re: 1638: "Backslashes"

Postby ps.02 » Wed Feb 03, 2016 6:45 am UTC

Eternal Density wrote:Breakdown of \\\[[(].*\\\[\])][^)\]]*$

Ah but that's not the real regex. Randall did this in the context of a Unix (well, I'm assuming Unix) shell, quoting the whole thing with "", so first you have to let the shell interpret the "" expression:

Original: "\\\[[(].*\\\[\])][^)\]]*$"
Sent to grep: \\[[(].*\\[\])][^)\]]*$
which means:
first \, then either [ or (, then any amount of text, then \, then ... hmmm, I'm sure he meant "either ] or )", but I don't think [\])] works. Normally one would omit the \. Finally, zero or more characters that are not ) or ], going to the end of the line.

Simpler:
grep -o '\\[[(].*\\[])].*' out.txt

Should do the same thing as what Randall probably meant. (His version probably didn't work, because of that stray \.)

_____
Posts: 5
Joined: Sun Dec 20, 2009 6:56 am UTC

Re: 1638: "Backslashes"

Postby _____ » Wed Feb 03, 2016 7:56 am UTC

I'm pretty sure that Randall was searching for MathJax equations.

User avatar
sfmans
Posts: 104
Joined: Mon Jun 23, 2014 9:09 am UTC
Location: High Peak, UK

Re: 1638: "Backslashes"

Postby sfmans » Wed Feb 03, 2016 8:19 am UTC

Is Randall suddenly doing a whole load more coding?

There's been a few recent cartoons around coding horrors, suggesting a mind being tortured by the stack of stack of stacks, the escaped escaped escaped backslash1, and other delights of life at the modern codeface.

I await BHG's take on polymorphism as a weapon for evil ...

1 Fun fact of the week, from a radio biography of David Bowie: one of his costume designers was Slash's mum, and Bowie remembered the infant Slash playing amongst all the jumpsuits and make-up and glitter.

roboticon
Posts: 1
Joined: Wed Feb 03, 2016 8:15 am UTC

Re: 1638: "Backslashes"

Postby roboticon » Wed Feb 03, 2016 8:24 am UTC

Depends what you call a "special" character, but:

Code: Select all

lines = []
with open('.bash_history') as f:
    lines = f.readlines()

rated_lines = []
for line in lines:
    if len(line) < 20: continue
    num_special = len([c for c in line if not (c.isalnum() or c.isspace())])
    rated_lines.append((num_special / float(len(line)), line))

rated_lines.sort(reverse=True)
for line in rated_lines[:10]:
    print line[1].strip()


With my top 2 being

Code: Select all

grep -i -o "md5:[a-f0-9:]*")
sed 's/[[:alnum:]]\{2\}/blah/' .git/config

User avatar
tejing
Posts: 9
Joined: Tue Dec 27, 2011 8:19 am UTC

Re: 1638: "Backslashes"

Postby tejing » Wed Feb 03, 2016 8:28 am UTC

Worst regular expression I ever wrote (ruby syntax)

Code: Select all

/\A\/((?:[^\/]*\\\/)*[^\/]*)\/((?:[^\/]*\\\/)*[^\/]*)\/\z/

What it does is actually surprisingly simple.

popman
Posts: 70
Joined: Sun Mar 07, 2010 7:38 pm UTC

Re: 1638: "Backslashes"

Postby popman » Wed Feb 03, 2016 8:29 am UTC

I see some other people noticed the "cat out.txt |" in the title text.
Anyone ever seen "tac| tac|" used in a script? I use it for holding onto any data piped in until a an EOF.

e.g. tac| tac| parallel wget
www.crashie8.com

savageorange
Posts: 16
Joined: Wed Aug 04, 2010 8:03 am UTC

Re: 1638: "Backslashes"

Postby savageorange » Wed Feb 03, 2016 9:01 am UTC

popman wrote:I see some other people noticed the "cat out.txt |" in the title text.
Anyone ever seen "tac| tac|" used in a script? I use it for holding onto any data piped in until a an EOF.

e.g. tac| tac| parallel wget


Not that I can think of. I would probably use sponge instead, as in "sponge | parallel wget"

User avatar
The Moomin
Posts: 358
Joined: Wed Oct 13, 2010 6:59 am UTC
Location: Yorkshire

Re: 1638: "Backslashes"

Postby The Moomin » Wed Feb 03, 2016 9:05 am UTC

I was expecting the program to come out with a bobcat.

Would not order from again.
I'm alive because the cats are alive.
The cats are alive because I'm alive.
Specious.

User avatar
Echo244
Posts: 511
Joined: Wed May 20, 2015 9:49 am UTC
Location: Ping! Ping! Ping! Ping!

Re: 1638: "Backslashes"

Postby Echo244 » Wed Feb 03, 2016 9:33 am UTC

sfmans wrote:I await BHG's take on polymorphism as a weapon for evil ...


How long has it been since we saw BHG? Even in Hoverboard, I saw a black hat, but nobody wearing one.
Unstoppable force of nature. That means she/her/hers.
Has committed an act of treason.

User avatar
orthogon
Posts: 3074
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1638: "Backslashes"

Postby orthogon » Wed Feb 03, 2016 10:31 am UTC

Quey wrote:

Code: Select all

cat out.txt |

Looks like someone needs a little demoggification? But who am I kidding? I do the same thing fairly often.

I don't see any problem with using cat to start a pipeline. It's much clearer what's going on, because in reality reading out the contents of the file(s) is the first step. Using '<' redirection to read the file puts the dramatis personae in the wrong order, as does providing the filename as an argument to grep. But the most important thing is that all the other commands in the pipeline work from stdin to stdout, so they're of the same form, and you can swap them around, remove commands and insert new ones without needing to change the individual bits. The overhead, one extra FIFO, is negligible, and in any case the whole thing will run six or seven orders of magnitude faster than doing it in, say, Windows.
xtifr wrote:... and orthogon merely sounds undecided.

popman
Posts: 70
Joined: Sun Mar 07, 2010 7:38 pm UTC

Re: 1638: "Backslashes"

Postby popman » Wed Feb 03, 2016 10:37 am UTC

savageorange wrote:Not that I can think of. I would probably use sponge instead, as in "sponge | parallel wget"

I heard of that, but it's not in my software sources right now.

sounds more like tee than anything though.
www.crashie8.com

User avatar
cellocgw
Posts: 2052
Joined: Sat Jun 21, 2008 7:40 pm UTC

Re: 1638: "Backslashes"

Postby cellocgw » Wed Feb 03, 2016 12:40 pm UTC

I heard that if you run that particular regex backwards on the text of a particular What-If column, it returns the sentence "Paul is dead."
https://app.box.com/witthoftresume
Former OTTer
Vote cellocgw for President 2020. #ScienceintheWhiteHouse http://cellocgw.wordpress.com
"The Planck length is 3.81779e-33 picas." -- keithl
" Earth weighs almost exactly π milliJupiters" -- what-if #146, note 7

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: 1638: "Backslashes"

Postby Rysto » Wed Feb 03, 2016 12:42 pm UTC

Isn't this all irrelevant because he forgot the -E flag to grep?

savageorange
Posts: 16
Joined: Wed Aug 04, 2010 8:03 am UTC

Re: 1638: "Backslashes"

Postby savageorange » Wed Feb 03, 2016 1:16 pm UTC

orthogon wrote:
Quey wrote:

Code: Select all

cat out.txt |

Looks like someone needs a little demoggification? But who am I kidding? I do the same thing fairly often.

I don't see any problem with using cat to start a pipeline. It's much clearer what's going on, because in reality reading out the contents of the file(s) is the first step. Using '<' redirection to read the file puts the dramatis personae in the wrong order,


Code: Select all

< out.txt grep -o '\\[[(].*\\[])].*'


It puts it in whatever order you want. You can even do bizarre stuff like

Code: Select all

grep -o -e < out.txt '\\[[(].*\\[])].*'

if you really want to.But I recommend not to want to.

as does providing the filename as an argument to grep

I guess?:

Code: Select all

grep out.txt -oe '\\[[(].*\\[])].*'


Rysto wrote:Isn't this all irrelevant because he forgot the -E flag to grep?


Clearly he has grep aliased to egrep ;)

User avatar
orthogon
Posts: 3074
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1638: "Backslashes"

Postby orthogon » Wed Feb 03, 2016 2:17 pm UTC

savageorange wrote:

Code: Select all

< out.txt grep -o '\\[[(].*\\[])].*'


It puts it in whatever order you want.

You mean you can ... and ... whoah. I need to sit down for a moment.
xtifr wrote:... and orthogon merely sounds undecided.

User avatar
jc
Posts: 353
Joined: Fri May 04, 2007 5:48 pm UTC
Location: Waltham, Massachusetts, USA, Earth, Solar System, Milky Way Galaxy
Contact:

Re: 1638: "Backslashes"

Postby jc » Wed Feb 03, 2016 2:20 pm UTC

ps.02 wrote:But hey! At least Randall M. didn't call "/" a backslash, as some are wont to do.


My favorite is when people call "/" a "forward slash". I'd guess that there's a geeky term for such uses of useless modifiers to cancel a prefix, but I don't know what it might be. (I've tried googling it, but only get zillions of hits on "prefix".)

User avatar
Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 4060
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: 1638: "Backslashes"

Postby Soupspoon » Wed Feb 03, 2016 2:47 pm UTC

It looks to me that the grep searches for {some or no leading text}\[({some or no middle text}\[]])]{some or no ending text, just as long as it doesn't have closing brackets of square or round kind in it}. The minimum required matching text being that in red and the blue stuff all entirely optional.

Ultimately that could be where an meta-square parenthetical set directly contains a regular parenthetical set with something that always ends with another meta-square (but empty) set.

out.txt is too vanilla a name to guess as to the origin (like I often wrote and ran DO.BAT files many, many times!), but could either be a subset of some code, or else some metadata normally further parsed by code where the meta-squares de-meta, on the way to another level, and maybe the non-meta ()s have a more immediate purpose. I don't think we can assume it's even Bash-code, in there, but it appears our user has no interest in ensuring that there's quotes in the match, so it needn't (but it could!) match lines in a file that were themselves valid forms of grep for perhaps another level of meta-ness within a tool-to-make-a-tool-to... that they are perhaps now trying to debug, work out how to improve or perhaps even reverse-engineer elements of.

User avatar
orthogon
Posts: 3074
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

Re: 1638: "Backslashes"

Postby orthogon » Wed Feb 03, 2016 2:55 pm UTC

jc wrote:
ps.02 wrote:But hey! At least Randall M. didn't call "/" a backslash, as some are wont to do.


My favorite is when people call "/" a "forward slash". I'd guess that there's a geeky term for such uses of useless modifiers to cancel a prefix, but I don't know what it might be. (I've tried googling it, but only get zillions of hits on "prefix".)

It's kind of like back-formation, but it's not quite that. One of my favourite non-words is configurate, which I think you could say was back-formed from confuguration.

In EN-GB (maybe in the US too?), a slash is a urination session, and we used to call the / character "stroke". When the WWW first reached these shores, some of the primmer announcers used to say, for example "bbc dot co dot uk stroke radio four". Unfortunately backstroke is something you do in a swimming pool, not a typesetting term.

ETA: retronym might be the word you're after.
xtifr wrote:... and orthogon merely sounds undecided.

ehmatthes
Posts: 1
Joined: Wed Feb 03, 2016 3:25 pm UTC

Re: 1638: "Backslashes"

Postby ehmatthes » Wed Feb 03, 2016 3:28 pm UTC

This is how Randall comments his code. He just writes code, then makes a comic about his code, then copies and pastes the comments from here into his code.

User avatar
Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 4060
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: 1638: "Backslashes"

Postby Soupspoon » Wed Feb 03, 2016 3:33 pm UTC

orthogon wrote:In EN-GB (maybe in the US too?), a slash is a urination session, and we used to call the / character "stroke". When the WWW first reached these shores, some of the primmer announcers used to say, for example "bbc dot co dot uk stroke radio four". Unfortunately backstroke is something you do in a swimming pool, not a typesetting term.


(Edit: This is a also a UK perspective... forgot to say... Of course I know the urination-term, too, but it wasn't often used around my area of a country that is famously diverse in both dialect and basic lexicon; and rarely would there be a context where the one use would be confusable with the other, I'm certain :oops: )

I can't remember what I called "/" immediately before the Web (I really should, I used it enough in other internet and Unix applications), or in the first few years of the '90s when it was still geeky, but I'm fairly sure that once the public got to know that it exists it was always started off as "aitch tee tee pee colon double-slash dubaya dubaya dubaya dot <whatever>" when being said in 'long-hand' (either because they didn't know enough to know that this was a likely given, except when it needed to be specified otherwise, or deliberately parodying and highlighting the geeky unwieldiness of the 'code' this system used).

(Edit2: Of course, some people tended to say "... double-backslash colon ...". And I'm fairly sure that quite early on (as early as NCSA Mosaic?) web-browsers would happily accept the likes of "http:\\www.website.com\subject\page.html" and silently (or visibly!) correct for this.)

I suspect that I might have long ago called "/" key either "divide" or the fuller "divide by" as I was reading it in mathematical contexts, for obvious reasons, especially as it didn't really have any other use in BBC BASIC, and I'm trying to remember whether it had any other use (like //-style commenting) in the various flavours of Pascal, COBOL, Forth, LISP, etc... I'm sure it was a comment-character in some of those, but I'm too rusty to bring it straight to mind.

ps.02
Posts: 378
Joined: Fri Apr 05, 2013 8:02 pm UTC

Re: 1638: "Backslashes"

Postby ps.02 » Wed Feb 03, 2016 4:33 pm UTC

_____ wrote:I'm pretty sure that Randall was searching for MathJax equations.

Ah, good catch.
cellocgw wrote:I heard that if you run that particular regex backwards on the text of a particular What-If column, it returns the sentence "Paul is dead."

Nice. (:
Rysto wrote:Isn't this all irrelevant because he forgot the -E flag to grep?

Either way. I don't see any extended regex syntax in there.
savageorange wrote:

Code: Select all

< out.txt grep -o '\\[[(].*\\[])].*'


It puts it in whatever order you want.

Putting redirects at the front of the line is still a little weird for me, but I do put 'em in the middle of a command. Take for example a Makefile, which has the limitation that it's ugly and confusing to continue a single shell command across multiple lines. Normally each line stands alone, so you can't use here-docs or other such tricks. Say you want to append several lines to an output file. The "traditional" approach looks something like this:

Code: Select all

        echo "First line" >> $@
        echo "Second line is much longer" >> $@
        echo >> $@
        echo "Last line" >> $@

But to me at least, the following looks ever so much clearer and nicer:

Code: Select all

        echo >>$@ "First line"
        echo >>$@ "Second line is much longer"
        echo >>$@
        echo >>$@ "Last line"

User avatar
Djehutynakht
Posts: 1546
Joined: Thu Feb 10, 2011 1:37 am UTC

Re: 1638: "Backslashes"

Postby Djehutynakht » Wed Feb 03, 2016 4:54 pm UTC

Oh hey, look. Ba'al the Soul-Eater has returned.

I think Randall's trying to tell us something.

Ray Kremer
Posts: 46
Joined: Fri Apr 26, 2013 8:21 pm UTC

Re: 1638: "Backslashes"

Postby Ray Kremer » Wed Feb 03, 2016 6:53 pm UTC

Ba'al? As in Bocce?

User avatar
Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 4060
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: 1638: "Backslashes"

Postby Soupspoon » Wed Feb 03, 2016 7:08 pm UTC

Not got a Bash shell handy, ATM, but I went to the old faithful Perl. There might well be differences.

Code: Select all

#!/bin/perl
use strict;
use warnings;

#my $match=q{\\\[[(].*\\\[\])][^)\]]*$};
## Didn't actually work, in the form of "$Test=~m/$match/", below...
## Couldn't be bothered to work out why, so I just migrated it down.

my @Tests = ('Nothing to see here',                                                 # No
             '\[(\[])]',                                                            # Minimum Yes?
             'foo\[(bar\[])]baz',                                                   # Expanded Yes?
             'foo\[(bar\[])]baz)',                                                  # Expanded but No?
             'Line example of \[(some information\[])] // perhaps with a comment'); # Should do...

foreach my $Test (@Tests) {
  print "Testing: \"$Test\"\n";
  if ($Test=~m/\\\[[(].*\\\[\]\)][^)\]]*$/) { print "\tMatched\n"   }
#                             ^ necessarily added
  else                                      { print "\tUnmatched\n" }
}

I needed to add that "necessarily added" extra backslash, for this to work without error, but it produces Unmatched, Matched, Matched, Unmatched, Matched, as expected.

User avatar
Solra Bizna
Posts: 59
Joined: Fri Dec 04, 2015 6:44 pm UTC

Re: 1638: "Backslashes"

Postby Solra Bizna » Wed Feb 03, 2016 7:30 pm UTC

I actually found a directory named \ in my home directory today, next to another named " and a file named -.d (as in hyphen period d).

savageorange wrote:It's doubly redundant since grep actually accepts a list of input files..

I had some bad experiences with `sed` once. Since then, I've defensively used pipelines even with programs that can take input files, in spite of the fact that this can actually create worse problems as with `do_something < some_file > some_file` (which is an excellent way to wipe out the contents of some_file and give `do_something` no input).

Rysto wrote:Isn't this all irrelevant because he forgot the -E flag to grep?

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

anrieff
Posts: 16
Joined: Thu Mar 19, 2009 8:34 pm UTC

Re: 1638: "Backslashes"

Postby anrieff » Wed Feb 03, 2016 10:42 pm UTC

And as usual for the internetz, there's a cat involved in this comic (not to mention the quite serious UNIXy discussion here) :lol:

ps.02
Posts: 378
Joined: Fri Apr 05, 2013 8:02 pm UTC

Re: 1638: "Backslashes"

Postby ps.02 » Wed Feb 03, 2016 10:58 pm UTC

Soupspoon wrote:Not got a Bash shell handy, ATM, but I went to the old faithful Perl. There might well be differences.

Indeed there might. Given the subject of this comic, it's worth noting that the most obvious differences in regex syntax between POSIX (grep) and Perl seem to revolve around the handling of, you guessed it, backslashes. Also, the fact that the (presumed Unix shell) original regex was quoted with " " means that there was some additional handling of - wait for it - backslashes. So while Perl may be of some help in experimentally working out theories on the intended meaning of the original, it's pretty much useless at helping you figure out what the original actually did (i.e., whether it "worked").

By the way, if you want to store a regex in a scalar variable in Perl, normally you use qr( ) rather than q( ). This may or may not have been why your first attempt failed.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: 1638: "Backslashes"

Postby phlip » Wed Feb 03, 2016 11:16 pm UTC

I feel like re-sharing my excessive-escaping story from the last time this topic came up:
phlip wrote:

Code: Select all

STR="`sed <<<"$STR" "s/\\\\(.\\\\)/\\\\1('\\\\\\\\s*\\\\\\\\+\\\\\\\\s*')?/g;s/('\\\\\\\\s\\\\*\\\\\\\\+\\\\\\\\s\\\\*')?$//"`"

Takes a word, say "foo", and builds a regex that will match things like:

Code: Select all

Text = 'This is a long string that includes f' +
  'oo but is long so it gets split over two lines'
which is then passed to grep. (The final regex in $STR would be "f('\s*\+\s*')?o('\s*\+\s*')?o"). This is preceded by about 10 lines of commenting defending the excessive backslashes.
(I've since learned about $() which does reduce the number of backslashes needed significantly)

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 4060
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: 1638: "Backslashes"

Postby Soupspoon » Thu Feb 04, 2016 12:27 am UTC

Regarding the -E
"In GNU grep, there is no difference in available functionality between basic and extended syntaxes."

But I'll test this when I'm not just on this Windows machine...

phlip wrote:I feel like re-sharing my excessive-escaping story from the last time this topic came up:

I should use sed more. Not a sentence I hear a lot. ;)

Although it tends to just make me obfuscate things even more (what do you mean "that's the whole point!"?) than I already get accused of doing with Perl.

Unobfuscated Perl version, though.

Code: Select all

#!/bin/perl
use strict;
use warnings;

my $searchfor="foo";
   print "Basically searching for [$searchfor]\n";

my $posLF='\n?'; # For the 'inline' testing, below.
# I know this is different from testing the source itself, like it appears you're doing,
# actually matching the "pausequote + newline continue" possible line-feed in code.
# For a direct replacement of what you're using it for, I think you'd use =q{('\s*\+\s*')?}
# (I'd consider optionally add a pattern to absorb any inline-comments, though.)

my $searchnew=join($posLF,split('',$searchfor));
# The money-shot.  A one-line split-and-rejoin, with the chosen 'optional linefeed' inserted.
   print "*Really* Searching for [$searchnew]\n";

my $test="This is a long string that includes f\n"
        ."oo but is long so it gets split over two lines\n";

print ($test=~m/$searchnew/m?"Y":"N");
# I don't use the //m option that much.  It might as well be //s
# Originally I was thinking of matching 'mid-string' ^$s, but it got silly.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: 1638: "Backslashes"

Postby phlip » Thu Feb 04, 2016 1:36 am UTC

Soupspoon wrote:Regarding the -E
"In GNU grep, there is no difference in available functionality between basic and extended syntaxes."

No difference in functionality, but a difference in backslash usage.

With plain grep, these are metacharacters: "*", "\+", "\(\)", "[]"
These are literals: "\*", "+", "()", "\[\]"

With grep -E, these are metacharacters: "*", "+", "()", "[]"
These are literals: "\*", "\+", "\(\)", "\[\]"

This is how (GNU) grep on my machine behaves, anyway.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

humanalien
Posts: 26
Joined: Sat Jan 12, 2008 9:11 pm UTC
Location: Minnesota
Contact:

Re: 1638: "Backslashes"

Postby humanalien » Thu Feb 04, 2016 2:26 am UTC

jc wrote:
ps.02 wrote:But hey! At least Randall M. didn't call "/" a backslash, as some are wont to do.


My favorite is when people call "/" a "forward slash". I'd guess that there's a geeky term for such uses of useless modifiers to cancel a prefix, but I don't know what it might be. (I've tried googling it, but only get zillions of hits on "prefix".)


https://xkcd.com/727/ is the first thing I thought of.
Image

I discovered a distance between two points that's shorter than a straight line! It's called a straight line segment.

http://www.abbyandnorma.com
http://www.erikahammerschmidt.com

RogueCynic
Posts: 403
Joined: Sun Nov 22, 2009 10:23 pm UTC

Re: 1638: "Backslashes"

Postby RogueCynic » Thu Feb 04, 2016 5:07 am UTC

In regards to the mouseover text, maybe he should rerun the command.
I am Lord Titanius Englesmith, Fancyman of Cornwood.
See 1 Kings 7:23 for pi.
If you put a prune in a juicer, what would you get?


Return to “Individual XKCD Comic Threads”

Who is online

Users browsing this forum: ZoomanSP and 90 guests