open knowledge community

Tuesday 27-12-2011 – 14u-18u – OpenToko Amersfoort
Learn how to stop worrying about and start loving /^1?$|^(11+?)\1+$/

Just after Christmas (December 27th at 14:00h) there will be an OpenToko.org workshop about Regular Expressions. The workshop will be given by Peter Uithoven who masters this super useful and timesaving technique for searching and replacing text.

Regular Expressions provide a concise and flexible means for searching (and replacing) text. It’s basically a very compact language to describes what you are searching for. You can use it for example to find or check e-mail addresses or links, automatically add lightbox or captions to images or for scraping websites. It let’s you do a lot of mundane things you normally would need to do by hand. You can use it in most programming languages and in a lot of text editors.

An example, the following script will give you all the alt tags from img tags in html, what ever is added inside the img element.

<img.*?alt=["'](.*?)["'].*?>

The plan is to give an introduction, give some nice references and tools and then freak with some practical challenges.

Even if you’ve never used regular expressions, this workshop will teach you all you need for most purposes.

- email or fill in the form to register
- Costs: please make a donation afterwards

RegExp Tool

Example html & javascript:
regexp tool

Results:

0. teveel regels / spaties weghalen

(\r|\s{2,})
/[\s\n\r]{2,}/ig
 {2,}
/  +/ /g
/^\s*$//
/\n\n/\n/
(\s)\1+
  replace for: $1

1. gratisophalen items ophalen (alleen aangeboden) http://www.gratisoptehalen.nl/

^\s*- <a href=”toon.php\?rid=\d+&status=aangeboden”>([^<]+)<.*\s.*\s.*
^.*status=aangeboden”>([^<]+)<.*\s.*\s.*
Twee stappen:
^.*status=aangeboden”>([^<]+).*\s.*\s.*
^[^A-Z].*\s
Korte PHP code voor Aangeboden items op gratisaftehalen.nl
————–MOOI———-
<?php
header(“Content-type: text/plain”);
$content = file_get_contents(“http://www.gratisoptehalen.nl”);
preg_match_all(“/status=aangeboden\”>(.*)<\/a>/”,$content,$matches);
print_r($matches);
?>
————————-
————-OOK MOOI——————–
$content = file_get_contents(“http://ofxaddons.com”);
preg_match_all(“/>(ofx[^<]+).*href=\”([^\"]*)\”/”,$content,$matches);
print_r($matches);
———————————

2. ofxaddons namen van addons (alles wat begint met ofx)

3. namen zoeken

4. “comma” seperated files ontleden (gescheiden op “,” of een tab, of een “”,”"

5. Ufo meldingen in comma separated file zetten

  UFO meldingen database: http://www.nuforc.org/webreports/ndxp111217.html

 

Links
Intro:
http://www.youtube.com/watch?v=DRR9fOXkfRE
http://www.youtube.com/watch?v=FCFdgymqpUI

References:
http://www.regular-expressions.info/
http://www.regular-expressions.info/reference.html
http://krijnhoetmer.nl/stuff/regex/cheat-sheet/
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
http://www.javascriptkit.com/javatutors/redev3.shtml

Tools:
http://gskinner.com/RegExr/
http://gskinner.com/RegExr/desktop/

Rick’s aantekeningen
Regular Expressions
———————————————————
Voor het zoeken en vervangen van tekst.

Handige tool om ‘ze’ te maken en te testen: http://gskinner.com/RegExr/

letters en lettercombinatieties zijn makkelijk
match: an zoekt naar het woordje an
replace by: the

match:
* [] voor een range aan karakters: [0-9.,gr] zoekt naar 0-9, punt, komma, g of r. Als je geen blokhaken zou doen zou hij zoeken naar de string 0-9.,gr
* [a-z] (afkorting \w)
* [0-9] (afkorting \d)
* hoofdletter om aan te geven dat je geen letters wil \W of geen cijfers \D
* [0-9.a-b] zoekt alle cijfers en alle letters
* [^*.,ba] het dakje zoekt naar alles behalve sterretje, punt, comma, b en a
* een ^ aan het begin van de match string (bijv. ^Amsterdam) zoekt alleen
* Amsterdam$ zoekt naar Amsterdam aan het einde van de string.
* global betekent dat ie alle instances zoekt.
* . is alles behalve een newline (wildcard)
* to|an|you to of an of you
* (to|an|you|include)en het woordje to of an of you of include maar alleen als het eindigd op en
* : \w* nu zoekt ie een dubbele punt of een spatie of een letter. ? is 0x of 1x, * is 0x of vaker, + is 1x of vaker.
“.*” zoekt alles wat tussen quotes staat behalve new-lines (. is alles behalve newline) Maar hij is wel greedy standaard dus hij zoekt te ver. Je kunt ‘m lazy maken met een vraagteken: “.*?” of beter “[^"]*” hij zoekt dan een dubbele quote. Vervolgens alles zonder dubbele quote en dat 0x of vaker.

nu wordt het leuk:
\[/?b\] vindt [b] of [/b]
\[list\] vindt [list]
\[\*\] vindt [*] maar als je [*] blabla wilt vervangen door

  • bla
  • doe je \[\*\](.*) vervangen door

  • $1
  • \[url=([^\]]+)\]([^\[]+)\[/url\] replace by $2
    [url=http://www.companje.nl]Ricksmuseum[/url]
    Ricksmuseum

    \b\w{2,}en\b vindt alle woorden die eindigen op en en 2 of meer letters voor ‘en’ hebben staan. De \b staat voor wordbound. Je zou ook spaties \s kunnen gebruiken maar krijgt dan problemen als je bijvoorbeeld twee worden achter elkaar hebt staan die dezelfde spatie delen (eerste eindigd op spatie, tweede begint met spatie)

    \1 is $1 maar dan voor gebruik in je matchpattern.

    Als je regular expressions wil gebruiken in bijv javascript moet je forward slashes gebruiken. /a/ig

    No Response

     

    tags

     
    © opentoko.org Proudly Powered by WordPress. Theme Untitled I Designed by Ruby Entries (RSS) and Comments (RSS).