Help Bites


  • Winner of the 2016 Presidential Election

    OK, I have a stupid small problem and I don't feel like it deserves its own thread. Maybe other people also have such issues. So I figured I'd start a "WTF Bites" style help thread.

    Anyway, I'm trying to do a stupid regex trick. This is a one-off, so I don't care how dirty or hacky it is - it's not going into a codebase anywhere. I just have a bunch of gross markup I am trying to clean en masse.

    Basically, I'm trying to do two things at once with this regex, either of which by itself is trivial. I need to:

    1. Match all the name="foo" in an HTML fragment. (OK to assume that all attributes are double-quoted.) /\bname="([^"]+)"/ See, I told you it was trivial.
    2. Remove all the hyphens in the names. /-/g

    Except I'm trying to do both of these tasks at once with a regex replace in my editor, and that's tricky. Basically, I want to do a find-and-replace within (restricted to) a regex find. To be clear, the whole match needs to be reinserted back into the document once the replacement is made.

    Of course, if my approach is back-assward and there's a much easier option, let me know that, too!

    Edit: Solved this more or less manually now, but still would like to know if this is possible, because it's not that uncommon of a task.



  • @error Which is the editor of choice? Most have a regex find and replace function


  • Winner of the 2016 Presidential Election

    @mrguyorama said in Help Bites:

    Which is the editor of choice?

    VS Code for the moment.

    @mrguyorama said in Help Bites:

    Most have a regex find and replace function

    Yes, the stated problem is how do I handle a recursive regex find and replace?

    In other words, find and find and replace?



  • @error shit wait yeah, I'm way out of my regex depth. I'd make a simple script to do this instead


  • Winner of the 2016 Presidential Election

    @mrguyorama said in Help Bites:

    @error shit wait yeah, I'm way out of my regex depth. I'd make a simple script to do this instead

    It seems possible because I can

    1. Find all instances of the text I want to restrict scope to.
    2. Have multiple selections at once in VS Code.
    3. Restrict find and replace to current selection.

    So all I'm really lacking is a bridge from 1 to 2 (i.e., select everything I matched in step 1, so it can be scoped for step 3).

    Edit: no, actually, while 2 and 3 are true, they don't seem to work together. I should file a bug for that, actually.



  • @error open one of those other editors in the meantime. They probably have that feature working



  • @error said in Help Bites:

    /\bname="([^"]+)"/

    Starting to think outloud-- should be something like "([^"-]+)?" so it captures multiple groups of not-quote-not-dash inside two quotes.



  • @error said in Help Bites:

    Solved this more or less manually now,

    I've found this is sometimes the most efficient way...



  • grep -r | sed > file; find *.code -exec sed -f file {} +



  • At times like these, what you need is a worse editor.
    M-x replace-regexp RET name="\([^"]+\)" RET name="\,(replace-regexp-in-string "-" "" \1)" RET



  • You know that the string has to be name="[some string not containing "]", so why not replace (name="[^"-]*)- with $1?

    Keep running it until it doesn't replace anything.


  • Discourse touched me in a no-no place

    @error said in Help Bites:

    would like to know if this is possible

    Not in a single operation; it's just a little bit beyond what that can do. Needs either scoped replacements or external looping.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.