Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,3 +1,8 @@
|
|||
{{omit from|BASIC}}
|
||||
{{omit from|Brlcad}}
|
||||
{{omit from|GUISS}}
|
||||
{{omit from|PARI/GP}}
|
||||
|
||||
The goal of this task is
|
||||
* to match a string against a regular expression
|
||||
* to substitute part of a string using a regular expression
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Regular expressions
|
||||
note: Text processing
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ void main() {
|
|||
immutable s = "I am a string";
|
||||
|
||||
// Test.
|
||||
if (!s.match(r"string$").empty)
|
||||
if (s.match("string$"))
|
||||
"Ends with 'string'.".writeln;
|
||||
|
||||
// Substitute.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
str1 = "This is a string!"
|
||||
str2 = "string"
|
||||
test = "My name is Lua."
|
||||
pattern = ".*name is (%a*).*"
|
||||
|
||||
print( str1:match( str2 ) )
|
||||
erg = str1:gsub( "a", "another" ); print( erg )
|
||||
if test:match(pattern) then
|
||||
print("Name found.")
|
||||
end
|
||||
|
||||
sub, num_matches = test:gsub(pattern, "Hello, %1!")
|
||||
print(sub)
|
||||
|
|
|
|||
39
Task/Regular-expressions/MAXScript/regular-expressions.max
Normal file
39
Task/Regular-expressions/MAXScript/regular-expressions.max
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
samples = #("Some string 123","Example text 123","string",\
|
||||
"ThisString Will Not Match","A123,333,string","123451")
|
||||
samples2 = #("I am a string","Me too.")
|
||||
|
||||
regex = dotnetobject "System.Text.RegularExpressions.Regex" ".*\bstring*"
|
||||
regex2 = dotnetobject "System.Text.RegularExpressions.Regex" "\ba\b"
|
||||
|
||||
clearlistener()
|
||||
|
||||
format "Pattern is : %\n" (regex.toString())
|
||||
|
||||
for i in samples do
|
||||
(
|
||||
if regex.ismatch(i) then
|
||||
(
|
||||
format "The string \"%\" matches the pattern\n" i
|
||||
)
|
||||
else
|
||||
(
|
||||
format "The string \"%\" doesn't match the pattern\n" i
|
||||
)
|
||||
)
|
||||
|
||||
-- replacement
|
||||
|
||||
format "Pattern is : %\n" (regex2.toString())
|
||||
|
||||
for i in samples2 do
|
||||
(
|
||||
if regex2.ismatch(i) then
|
||||
(
|
||||
local replaced = regex2.replace i "another"
|
||||
format "The string \"%\" matched the pattern, so it was replaced: \"%\"\n" i replaced
|
||||
)
|
||||
else
|
||||
(
|
||||
format "The string \"%\" does not match the pattern\n" i
|
||||
)
|
||||
)
|
||||
|
|
@ -2,8 +2,8 @@ import re
|
|||
|
||||
string = "This is a string"
|
||||
|
||||
if re.search('string$',string):
|
||||
if re.search('string$', string):
|
||||
print("Ends with string.")
|
||||
|
||||
string = re.sub(" a "," another ",string)
|
||||
print string
|
||||
string = re.sub(" a ", " another ", string)
|
||||
print(string)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,8 @@
|
|||
/*REXX program demonstrates testing (modeled after Perl example).*/
|
||||
$string = "I am a string"
|
||||
say 'The string is:' $string
|
||||
x = "string"
|
||||
if right($string,length(x))=x then say 'It ends with:' x
|
||||
|
||||
y = "You"
|
||||
if left($string,length(y))\=y then say 'It does not start with:' y
|
||||
|
||||
z = "ring"
|
||||
if pos(z,$string)\==0 then say 'It contains the string:' z
|
||||
|
||||
z = "ring"
|
||||
if wordpos(z,$string)==0 then say 'It does not contain the word:' z
|
||||
$string="I am a string"
|
||||
say 'The string is:' $string
|
||||
x="string" ; if right($string,length(x))=x then say 'It ends with:' x
|
||||
y="You" ; if left($string,length(y))\=y then say 'It does not start with:' y
|
||||
z="ring" ; if pos(z,$string)\==0 then say 'It contains the string:' z
|
||||
z="ring" ; if wordpos(z,$string)==0 then say 'It does not contain the word:' z
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
$string = "I am a string"
|
||||
old = " a "
|
||||
new = " another "
|
||||
say 'The original string is:' $string
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
say 'The original string is:' $string
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
$string = changestr(old,$string,new)
|
||||
say 'The changed string is:' $string
|
||||
say 'The changed string is:' $string
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ $string = "I am a string"
|
|||
old = " a "
|
||||
new = " another "
|
||||
say 'The original string is:' $string
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
$string2 = changestr(old,$string,new)
|
||||
say 'The original string is:' $string
|
||||
say 'The changed string is:' $string2
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
old = " am "
|
||||
new = " was "
|
||||
say 'The original string is:' $string
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
say 'old word is:' old
|
||||
say 'new word is:' new
|
||||
|
||||
if wordpos(old,$string)\==0 then
|
||||
if wordpos(old,$string)\==0 then
|
||||
do
|
||||
$string = changestr(old,$string,new)
|
||||
say 'I was able to find and replace ' old " with " new
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
s="I am a string"
|
||||
if [[ $s =~ str..g$ ]]; then
|
||||
echo "the string ends with 'str..g'"
|
||||
fi
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
s="I am the original string"
|
||||
re='o.*l'
|
||||
repl="modified"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
modified=${s/~(E)$re/$repl}
|
||||
echo "$modified" # I am the modified string
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
if [[ $s =~ $re ]]; then
|
||||
submatch=${BASH_REMATCH[0]}
|
||||
modified="${s%%$submatch*}$repl${s#*$submatch}"
|
||||
echo "$modified" # I am the modified string
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue