Difference between revisions of "Regex"
From Wiki2
Line 1: | Line 1: | ||
==regex== | ==regex== | ||
to take out all non alpha characters | to take out all non alpha characters (Ruby) | ||
<code> | |||
test_str = "Madam, I'm Adam" | test_str = "Madam, I'm Adam" | ||
str = test_str.gsub(/[^a-zA-Z]/,'') | str = test_str.gsub(/[^a-zA-Z]/,'') | ||
puts str | puts str | ||
</code> | |||
returns MadamImAdam | returns MadamImAdam | ||
Revision as of 22:03, 9 August 2013
regex
to take out all non alpha characters (Ruby)
test_str = "Madam, I'm Adam"
str = test_str.gsub(/[^a-zA-Z]/,)
puts str
returns MadamImAdam
- ^[ \t]+ //finds all the space and tabs
- [1-9]\. //finds all the line numbers. (replace with #)
- [A-E]\. //find A. etc (replace with ##)
to clean special characters from a string
$clean = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $text);
sites
- http://rubular.com
- http://e-texteditor.com/blog/2010/beyond-vi
- http://www.grymoire.com/Unix/Regular.html#uh-2
- http://www.regular-expressions.info/php.html
expressions
If you want to get'Aloha World'out
Input:
Hello World
Aloha World
Hey There
RegEx:
\<div\sclass\=\"somename\"\>(?<Text>.*?)\<\/div\>
Yields:
Aloha World (note: In a single group named Text)