Jump to content
Sign in to follow this  
Noddle

Help with Regular Expression

Recommended Posts

Noddle

Hi,

can someone help me with an regular expression

I want to remove all numbers on the   right hand side, up to the last letter or ()

                   
thecat45satonethemat25624                         remove 25624
or 123 thecatisextra(fat)4759                         remove 4759
orababy-cat hasextra.9lives450813458         remove 450813458
or-2_dog eatsfat(.cats)846395                      remove 846395
or44catcaneat234lots23                                remove 23

etc..

I was thinking,  reading the  string from the right to left,
and the first character that is not a number would be the end of the string,
then do a stringleft (string, stringlen - (count right to left for first non number)

but im sure StringRegExpReplace with the correct patter would be so much cleaner, faster

thanks for any help on the "expression"

Nigel

Share this post


Link to post
Share on other sites
kylomas

Noodle,

Try this...

#include<array.au3>

local $str = 'thecat45satonethemat25624' & @crlf
$str &= '123 thecatisextra(fat)4759' & @crlf
$str &= 'ababy-cat hasextra.9lives450813458' & @crlf
$str &= '-2_dog eatsfat(.cats)846395' & @crlf
$str &= '-2_dog eatsfat(.cats)846395withfollowingtext' & @crlf
$str &= '44catcaneat234lots23' & @crlf

local $o_str = stringregexp($str,'(?m)(\S.*?)\d{0,}$',3)

_arraydisplay($o_str)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
jchd

Something like that?

#include  <Array.au3>

Local $aTest = [ _
    "thecat45satonethemat25624", _
    "123 thecatisextra(fat)4759", _
    "ababy-cat hasextra.9lives450813458", _
    "-2_dog eatsfat(.cats)846395", _
    "44catcaneat234lots23" _
]

For $i = 0 To UBound($aTest) - 1
    $aTest[$i] = StringRegExpReplace($aTest[$i], "(\d*)$", "")
Next

_ArrayDisplay($aTest)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jchd

kylomas,

You're removing blank spaces, tabs and such at the head of lines.

#include<array.au3>

local $str = 'thecat45satonethemat25624' & @crlf
$str &= '123 thecatisextra(fat)4759' & @crlf
$str &= '        ababy-cat hasextra.9lives450813458' & @crlf
$str &= '-2_dog eatsfat(.cats)846395' & @crlf
$str &= '-2_dog eatsfat(.cats)846395withfollowingtext' & @crlf
$str &= '44catcaneat234lots23' & @crlf

local $o_str = stringregexp($str,'(?m)(^.*?)\d*$',3)

_arraydisplay($o_str)
Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
kylomas

@jchd - Thanks, I knew the pattern was simpler than what I was doing but could not figure out how to get rid of the blank lines in the result array.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
Noddle

This is what I used in the end,  a little of both,

#include<array.au3>

local $str = 'thecat45satonethemat25624' & @crlf
$str &= '123 thecatisextra(fat)4759' & @crlf
$str &= '        ababy-cat hasextra.9lives450813458' & @crlf
$str &= '-2_dog eatsfat(.cats)846395' & @crlf
$str &= '-2_dog eatsfat(.cats)846395withfollowingtext' & @crlf
$str &= '44catcaneat234lots23' & @crlf

$aTest = StringSplit($str,@cr)

For $i = 0 To UBound($aTest) - 1
    $aTest[$i] = StringRegExpReplace($aTest[$i], "(\d*)$", "")
Next

_arraydisplay($aTest)

Thanks for your help,

Nigel

Edited by Noddle

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×