Difference between revisions of "Remove non-letters from a string"

From CodeCodex

m
Line 25: Line 25:
 
}
 
}
 
}
 
}
 +
</HIGHLIGHTSYNTAX>
 +
 +
===JavaScript===
 +
<HIGHLIGHTSYNTAX language="javascript">
 +
var s = "how19 a*re 254y**ou?";
 +
 +
document.write(s.replaceAll(/[^A-Za-z]/, ""));
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
  
Line 37: Line 44:
 
<HIGHLIGHTSYNTAX language="perl">
 
<HIGHLIGHTSYNTAX language="perl">
 
  s{[\W\d_]}{}g; # remove all non-word characters and digits and underscores
 
  s{[\W\d_]}{}g; # remove all non-word characters and digits and underscores
 +
</HIGHLIGHTSYNTAX>
 +
 +
===PHP===
 +
<HIGHLIGHTSYNTAX language="php3">
 +
$result = preg_replace('/[^A-Za-z]/', '', $string);
 
</HIGHLIGHTSYNTAX>
 
</HIGHLIGHTSYNTAX>
  
 
===Python===
 
===Python===
<pre>
+
<HIGHLIGHTSYNTAX language="python">
 
from string import letters
 
from string import letters
 
s = "hello world! how are you? 0"
 
s = "hello world! how are you? 0"
Line 51: Line 63:
 
tostrip = "".join(c for c in id_tab if c not in letters)
 
tostrip = "".join(c for c in id_tab if c not in letters)
 
print s.translate(id_tab, tostrip)
 
print s.translate(id_tab, tostrip)
</pre>
+
 
 +
# Using regular expressions
 +
print re.sub("[^A-Za-z]", "", s)
 +
</HIGHLIGHTSYNTAX>
  
 
=== Tcl ===
 
=== Tcl ===
Line 73: Line 88:
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:C]]
 
[[Category:C]]
 +
[[Category:JavaScript]]
 
[[Category:Objective Caml]]
 
[[Category:Objective Caml]]
 
[[Category:Perl]]
 
[[Category:Perl]]
 +
[[Category:PHP]]
 
[[Category:Python]]
 
[[Category:Python]]
 
[[Category:Tcl]]
 
[[Category:Tcl]]
 
[[Category:String]]
 
[[Category:String]]
 
[[Category:Visual Basic]]
 
[[Category:Visual Basic]]

Revision as of 06:14, 20 July 2008

Related content:

Implementations

Java

This code removes common symbols and numbers from a string, returning letters only. (Note, not all symbols are included.)

static String lettersOnly(String s) {
   return s.replaceAll("[^a-zA-Z]","");
}

C

if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then the content of buff_02 will be: "HelloWorldHowAreYou" <HIGHLIGHTSYNTAX language="c">#include <string.h>

while (i != strlen (buff)) { if ((buff[i] >= 65) && (buff[i] <= 90) || (buff[i] >= 97) && (buff[i] <= 122)) { buff_02[j] = buff[i]; i++; j++; } else { i++; } } </HIGHLIGHTSYNTAX>

JavaScript

<HIGHLIGHTSYNTAX language="javascript"> var s = "how19 a*re 254y**ou?";

document.write(s.replaceAll(/[^A-Za-z]/, "")); </HIGHLIGHTSYNTAX>

OCaml

# let remove_nonalpha = Str.global_replace (Str.regexp "[^a-zA-Z]+") "";;
val remove_nonalpha : string -> string = <fun>

For example:

# remove_nonalpha "133t H4x0r";;
- : string = "tHxr"

Perl

<HIGHLIGHTSYNTAX language="perl">

s{[\W\d_]}{}g; # remove all non-word characters and digits and underscores

</HIGHLIGHTSYNTAX>

PHP

<HIGHLIGHTSYNTAX language="php3"> $result = preg_replace('/[^A-Za-z]/', , $string); </HIGHLIGHTSYNTAX>

Python

<HIGHLIGHTSYNTAX language="python"> from string import letters s = "hello world! how are you? 0"

  1. Short version

print filter(lambda c: c in letters, s)

  1. Faster version for long ASCII strings:

id_tab = "".join(map(chr, xrange(256))) tostrip = "".join(c for c in id_tab if c not in letters) print s.translate(id_tab, tostrip)

  1. Using regular expressions

print re.sub("[^A-Za-z]", "", s) </HIGHLIGHTSYNTAX>

Tcl

proc remove_non_ascii_letters s {regsub -all {[^a-zA-Z]} $s ""}
remove_non_ascii_letters "hello world! how are you? 0é" ;# -> helloworldhowareyou

Visual Basic

Public Function ReturnAlpha(ByVar sString As String) As String
Dim i As Integer
For i = 1 To Len(sString)
    If Mid(sString, i, 1) Like "[A-Za-z]" Then
        ReturnAlpha = ReturnAlpha + Mid(sString, i, 1)
    End If
Next i
End Function