The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Archive for the ‘RegEx’ Category

sed double expression: match, replace in one line, overwrite file

Posted by jpluimers on 2020/04/15

A while ago, I needed to conditionally replace in files, so I used sed and a regular expression, though usually I dislike those.

However, since the system had a very basic install, there was not much choice.

Luckily back then, my Google foo returned these:

This allowed me to do a double expression (the first matches a pattern, the second performs the actual replacement within the matching lines).

In case my Google foo in the future fails:

## https://robots.thoughtbot.com/sed-102-replace-in-place
## -i causes no backup to be saved, but does in-place replacement
## since we run under git, we can always restore
## combined with a double expression (the first matches, the second executes) this is very powerful
sed -i -e '/#.*AVOID_DAILY_AUTOCOMMITS=.*$/s/^.//' /etc/etckeeper/etckeeper.conf && git diff | more

–jeroen

Posted in *nix, Development, etckeeper, Linux, Power User, RegEx, Software Development | Leave a Comment »

GExperts: searching for case-insensitive “T*List.Create” but not “TStringList.Create”

Posted by jpluimers on 2019/05/02

Just learned that partial exclusion can be done with the case-insensitive GExperts Grep Search like this:

T[^s][^t][^r][^i][^n][^g].*List.*\.Create

This will skip TStringList.Create, but matches TMyList.Create.

I’d rather have done something like this, but the Delphi RegEx does not support negative lookbehind:

^ *[a-zA-Z0-9_]* *: *T(<!string)[a-zA-Z0-9_]*ListO? *;$

So the alternative is to search for this:

^ *[a-zA-Z0-9_]* *: *T[a-zA-Z0-9_]*ListO? *;$

then exclude all the case insensitive TStringList entries from it, however GExperts did not support that at the time of writing.

This is an intermediate that works for some of the times:

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *;$

–jeroen

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *$;
^ *[a-zA-Z0-9_]*: .T[a-zA-Z0-9_]*ListO? *;$;
^ *[a-zA-Z0-9_]* *: *T(?!string)[a-zA-Z0-9_]*ListO? *;$

Posted in Delphi, Development, GExperts, RegEx, Software Development | Leave a Comment »

RegExr: Online Regular Expression Testing Tool

Posted by jpluimers on 2018/11/19

[WayBack] Kevlin Henney – Google+ reminded me of RegExr. Then I found out I collected the below links in 2010, but never published them. Hopefully a few still exist. Let’s see… yup: all do, but archived just in case.

Some other links (that show how much the landscape changed in 8.5 years time):

–jeroen

Posted in Development, RegEx, Software Development | Leave a Comment »

aha (Ansi HTML Adapter) with clickable URIs

Posted by jpluimers on 2018/10/02

aha is great to generate HTML from ANSI text (i.e. the coloured output on a Linux console).

But it doesn’t generate clickable URIs (it can’t yet by itself as it only looks one character in the future).

The thread at https://github.com/theZiz/aha/issues/20 suggested a case-insensitive regex through sed but the exact suggestion failed for a few reasons I will explain below.

First the bash alias (requires both aha and perl):


#!/usr/bin/env bash
# based on https://github.com/theZiz/aha/issues/20#event-797466520
aha-with-expanded-http-https-urls()
{
aha | perl -C -Mutf8 -pe 's,([^"])((https?|s?ftp|ftps?|file)://.*?)([\s]|\&quot;\s),$1<a href="$2">$2</a>$4,gi'
}

Read the rest of this entry »

Posted in *nix, *nix-tools, bash, bash, Development, Perl, Power User, RegEx, Scripting, Software Development | Leave a Comment »

Regular Expression Crossword Puzzle – Gregable

Posted by jpluimers on 2018/09/26

A crossword puzzle designed for geeks, every single clue is a regular expression.

Source: [WayBackRegular Expression Crossword Puzzle – Gregable

No matter your take on RegEx, I think you will be amazed just looking at it.

Related:

Via:

–jeroen

Read the rest of this entry »

Posted in Development, RegEx, Software Development | Leave a Comment »

Regex – Does not contain certain Characters – Stack Overflow

Posted by jpluimers on 2018/06/01

I had to find strings containing ” of object”, but not starting with a “)”, so the regex became this:

[^)] of object

Source: [WayBackRegex – Does not contain certain Characters – Stack Overflow

–jeroen

Posted in Development, RegEx, Software Development | Leave a Comment »

Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.

Posted by jpluimers on 2018/01/18

On my list of things to try out when I need to use regular expressions again:

Test your regex by visualizing it with a live editor. JavaScript, Python, and PCRE.

Source: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.

Via: [WayBackSergiu Toarca answering [WayBackRegex lookahead with multiple negative conditions – Stack Overflow

Note: in January 2018, Debuggex was down for a few days (Thanks Uwe for bringing that up and giving the below alternative [WayBack]).

If you are on Windows and are OK with a small cost, try this instead: [WayBack] RegexBuddy: Learn, Create, Understand, Test, Use and Save Regular Expression

–jeroen

Posted in Development, RegEx, Software Development | Leave a Comment »

Convert space indented text to markdown minus indented

Posted by jpluimers on 2018/01/17

Since I will never be good at regex, this are a few search/replace patterns I used in Atom.io to convert a plain text document space indented like this:

Version 1.23.4.15
First level indented text
Is also a first level indented text
Too a first level indented text
- First level indented text too
  Second level indented text
  Is also a second level indented text
  Too a second level indented text
  - Second level indented text too
    - A third level indented text
    Third level indented text
    Is also a third level indented text
    Too a third level indented text
98.4.32.1

to a Markdown indented one like this:

Version 1.23.4.15
- First level indented text
- Is also a first level indented text
- Too a first level indented text
- First level indented text too
 - Second level indented text
 - Is also a second level indented text
 - Too a second level indented text
 - Second level indented text too
 - A third level indented text
 - Third level indented text
 - Is also a third level indented text
 - Too a third level indented text
Version 98.4.32.1

Prepend Version when needed: https://regex101.com/r/CUhUbr/1

  • Search:
    • ^(\d+\.\d+\.\d+\.\d+)
  • Replace:
    • Version $1

Add markdown first level indentations to lines that don’t have a markdown first level indentation yet, nor start with Version nor start with a space: https://regex101.com/r/CUhUbr/2

  • Search:
    • ^(?!(Version )|(- )|( )|(\d+\.\d+\.\d+\.\d+))(.*)
  • Replace:
    • - $5

Add markdown second level indentations to lines that don’t have a markdown second level indentation yet but do have a regular second level indentation: https://regex101.com/r/CUhUbr/3

  • Search:
    • ^  (?!(- )|( ))(.*)
  • Replace:
    •   - $3

Add markdown third level indentations to lines that don’t have a markdown third level indentation yet but do have a regular third level indentation: https://regex101.com/r/CUhUbr/4

  • Search:
    • ^    (?!(- )|( ))(.*)
  • Replace:
    •     - $3

jeroen

https://regex101.com/r/CUhUbr/1

https://regex101.com/r/CUhUbr/2

https://regex101.com/r/CUhUbr/3

https://regex101.com/r/CUhUbr/4

 

Posted in Development, RegEx, Software Development | Leave a Comment »

regex – Regular expression to match DNS hostname or IP Address? – Stack Overflow

Posted by jpluimers on 2017/08/29

I’m not fond of them, but sometimes they can do their job regex – Regular expression to match DNS hostname or IP Address? – Stack Overflow

Two notes:

  1. The first answer matches either IPv4 or hostname
    (no IPv6 support yet; reminder to self: find it one day).
  2. The second answer restricts the hostname parts to be no more than 63 characters each. It doesn’t fix the total length being 255 or less
    (reminder to self: create that restriction one day).

First answer:

Read the rest of this entry »

Posted in Development, RegEx, Software Development | Leave a Comment »

JavaScript. Sigh. No real RegExp support. Sigh. Google Search results. Sigh.

Posted by jpluimers on 2017/03/01

Prologue

Every time I need to use JavaScript there’s this tiny voice in the back of my head “Please don’t”, for instance because of

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=.

Verify a URI in JavaScript with a Regular Expression using Google Search examples

This time it did it again: I used JavaScript. My need was to verify a basic URI in JavaScript, so I wrote this function based on RFC 3986 [WayBack] which in Appendix B has a nice regular expression: ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

function isValidUri(uri){
    var uriRegExPattern = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?";
    var uriRegEx = new RegExp(uriRegExPattern); 

    return (uriRegEx.test(uri));
} 

It would crash. But JavaScript is JavaScript, so even a site like JSFiddle wouldn’t show an error (later I found out that enabling the console on http://jsbin.com/wamavacuco/edit?html,console,output does show the error in the console complete with stack trace).

Read the rest of this entry »

Posted in Development, JavaScript/ECMAScript, JSFiddle, JSON, RegEx, Scripting, Software Development | Leave a Comment »