firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.28k stars 199 forks source link

Add "sed" and "awk" to code generator #272

Closed federico-piazza closed 4 years ago

federico-piazza commented 9 years ago

It would be nice to have in Code Generator the sample for sed and awk

mskadu commented 9 years ago

Or grep - possibly the most frequent command regexps arenused with.

firasdib commented 7 years ago

I'd be happy to if someone could clear up the "rules" for each flavor, so I can try to make the output as nice as possible.

federico-piazza commented 7 years ago

@firasdib As far as I know, grep and sed have parameters to support PCRE regex. In case of grep I think it is grep -P and in case of sed it is sed -r (-E in mac). Also I think there is ssed for full pcre support.

devnoname120 commented 7 years ago

It would be very useful, it's really painful to escape the regex string for sed. Edit never mind, using single quotes solves this issue.

kanihal commented 5 years ago

for sed, +1 It would be very useful.

firasdib commented 5 years ago

Can anyone write the codegen for this? See https://github.com/firasdib/Regex101/wiki/Writing-a-Code-Generator

phdoerfler commented 5 years ago

sed, awk, grep

I would recommend not doing awk and focussing solely on sed for now. Awk is a very different beast and sed should be installed pretty much everywhere where there is also awk. I would also avoid grep for now. Whilst it at first sounds like the goto regex evaluator for the cmdline in my personal experience it has a lot of unexpected limitations when it comes to using it with extended regex.

sed and macOS

Keep in mind that on macOS the default sed is from BSD which has quite different behaviour and flags. Don't bother supporting that old thing. Those who really need old BSD sed hopefully know how to use it anyway. Instead simply use gsed instead of sed. It gives you the ubiquitous GNU sed with all the familiar flags and behaviour. It can be easily installed with brew install gnu-sed.

sed as a drop-in regex evaluator

My go to flags for GNU sed which allow me to basically copy paste RegEx from Regex101 are:

gsed -r 's/YOUR_REGEX_GOES_HERE/YOUR_SUBSTITUTION_GOES_HERE/FLAGS_GO_HERE;t;d'

# Examples
gsed -r '/Amperage/s/^.+\s+([-0-9]+)\s*$/\1/;t;d'
sed -r '/cd/s/.+;\s*cd\s+(.+)/\1/;t;d'
history 1 | sed 's/^ *[0-9]* *//' |

Here is what -r does:

       -E, -r, --regexp-extended

              use extended regular expressions in the script (for portability use POSIX -E).

As to ;t and ;d: Those are sed commands that I do not dare to touch. I grandfathered these from a more seasoned sed veteran and I have had 0 problems with them but a great many problems without them. If you care to learn more, you can do so from the GNU documentation but be warned.

phdoerfler commented 5 years ago

I made a first draft of a sed bash code generator. Please help yourself to adding proper escaping because right now it just hopes there is no ' in there.

class Sed extends PureComponent {
  static propTypes = {
    regex: PropTypes.string.isRequired,
    flags: PropTypes.string.isRequired,
    delimiter: PropTypes.string.isRequired,
    testString: PropTypes.string.isRequired,
    isSubstituting: PropTypes.bool.isRequired,
    isGlobal: PropTypes.bool.isRequired,
    substString: PropTypes.string
  };

  render() {
    return (
      <Highlight lang="sed">
        {this._renderCode()}
      </Highlight>
    );
  }

  _renderCode() {
    const { regex, flags, delimiter, testString, isSubstituting, substString, isGlobal } = this.props;

    const codeString = new CodeString();

    if (! isSubstituting) {
      codeString.append("This generator only supports substitution. Please update your regular expression accordingly");
      return codeString.toString();
    }

    codeString.append(`sed -E \'s/${regex}/${substString}/${flags};t;d\' <<< "${testString}" `);

    return codeString.toString();
  }

}

export default Sed;
firasdib commented 4 years ago

Wow, I've totally missed this. Many thanks @phdoerfler

I'll add this to my todo list.

Doqnach commented 3 years ago

@guest271314 please join the chat (IRC) if you are looking for this type of help. Your question is kind of outside the scope of GitHub issues, and you commented on a long closed issue.