Jump to content

stringregexpreplace question


 Share

Recommended Posts

argh, pulling my hair out.

considering this post: 

 

say for a string = "03a", how can I strip out the leading 0 and the a.

I have tried:

$new = StringRegExpReplace($string, '[^1-9][^0-9]', '')

 

and various combinations:

^0+[^0-9]

[^[:digit:]]

"[^0].*"

"^0*(d+)"

 

I'm going loopy!

 

 

Link to comment
Share on other sites

This one will remove leading zero(s) and trailing non digit(s) - if exist

$new = StringRegExpReplace($string, '^0*|\D*$', "")

Edit : comments
 You want your regex to have 2 different actions, so an alternation is needed
^0*  : zero or more "0" at the beginning of the string
|       : alternation
\D*$   :  zero or more non-digit characters at the end of the string

Edited by mikell
Link to comment
Share on other sites

@mikell& @FrancescoDiMuro I am sure a single file that resuming all delimiter whould be apreciate from everyone. I know the help file is already here but... even for people who want to learn this part is hard.  

 

So yeah comment like this is realy appreciated.

 

In help file it is like that : 
 

^0*  : zero or more digit at the beginning of the string

(?: ... )   Non-capturing group. Does not record the matching characters in the array and cannot be re-used as backreference.
\D

Matches any non-digit.
[^ ... ]    Matches any character not in the set: "[^0-9]" matches any non-digit. To include a caret (^) in a set, put it after the beginning of the set or escape it (\^).

But we dont got that with exemple at each delimiter :

^0*  : zero or more "0" at the beginning of the string
|       : alternation
\D*$   :  zero or more non-digit characters at the end of the string

It whould simplifi the life of all beginner. Since i am not strong at it i cannot do that :( 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

not for nothing, but if your only criteria are stripping leading zeroes and removing trailing non-number characters

$new = number("03a")

 

Edited by iamtheky
grammar

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

iamtheky,
Very smart non-regex way  :D
I made a regex-oriented answer because ... of the title of the topic. Nothing personal  :P


caramen,
The help file provides explicit details and explanations about the syntax, and should constantly be used as reference and be read carefully to avoid confusion
Examples :

^0*  : zero or more digit at the beginning of the string

This is false. "0" means only 0 . A digit is \d, or [0-9], or [[:digit:]]  (all the same)

Ditto for ^ , which matches "start of string" but is also used inside a character class to negate its content
So to match any non-digit, you might use \D , or [^0-9] , or  [[:^digit:]]

Link to comment
Share on other sites

On 22/10/2018 at 2:19 PM, mikell said:

A digit is \d, or [0-9], or [[:digit:]]  (all the same)

Not exactly all the same.

When/if (*UCP) is used, [0-9] does exactly what it means (ASCII 0 to ASCII 9) while both other forms include codepoins that have the Nd (Numeric Decimal) properties. And that is a number of characters: from old Unicode v5.1:

Codepoint    Glyph    CharacterName
U+00030     '0'     DIGIT ZERO
U+00660     '٠'     ARABIC-INDIC DIGIT ZERO
U+006F0     '۰'     EXTENDED ARABIC-INDIC DIGIT ZERO
U+007C0     '߀'     NKO DIGIT ZERO
U+00966     '०'     DEVANAGARI DIGIT ZERO
U+009E6     '০'     BENGALI DIGIT ZERO
U+00A66     '੦'     GURMUKHI DIGIT ZERO
U+00AE6     '૦'     GUJARATI DIGIT ZERO
U+00B66     '୦'     ORIYA DIGIT ZERO
U+00BE6     '௦'     TAMIL DIGIT ZERO
U+00C66     '౦'     TELUGU DIGIT ZERO
U+00CE6     '೦'     KANNADA DIGIT ZERO
U+00D66     '൦'     MALAYALAM DIGIT ZERO
U+00E50     '๐'     THAI DIGIT ZERO
U+00ED0     '໐'     LAO DIGIT ZERO
U+00F20     '༠'     TIBETAN DIGIT ZERO
U+01040     '၀'     MYANMAR DIGIT ZERO
U+01090     '႐'     MYANMAR SHAN DIGIT ZERO
U+017E0     '០'     KHMER DIGIT ZERO
U+01810     '᠐'     MONGOLIAN DIGIT ZERO
U+01946     '᥆'     LIMBU DIGIT ZERO
U+019D0     '᧐'     NEW TAI LUE DIGIT ZERO
U+01B50     '᭐'     BALINESE DIGIT ZERO
U+01BB0     '᮰'     SUNDANESE DIGIT ZERO
U+01C40     '᱀'     LEPCHA DIGIT ZERO
U+01C50     '᱐'     OL CHIKI DIGIT ZERO
U+024EA     '⓪'     CIRCLED DIGIT ZERO
U+024FF     '⓿'     NEGATIVE CIRCLED DIGIT ZERO
U+0A620     '꘠'     VAI DIGIT ZERO
U+0A8D0     '꣐'     SAURASHTRA DIGIT ZERO
U+0A900     '꤀'     KAYAH LI DIGIT ZERO
U+0AA50     '꩐'     CHAM DIGIT ZERO
U+0FF10     '0'     FULLWIDTH DIGIT ZERO
U+104A0     '𐒠'     OSMANYA DIGIT ZERO
U+1D7CE     '𝟎'     MATHEMATICAL BOLD DIGIT ZERO
U+1D7D8     '𝟘'     MATHEMATICAL DOUBLE-STRUCK DIGIT ZERO
U+1D7E2     '𝟢'     MATHEMATICAL SANS-SERIF DIGIT ZERO
U+1D7EC     '𝟬'     MATHEMATICAL SANS-SERIF BOLD DIGIT ZERO
U+1D7F6     '𝟶'     MATHEMATICAL MONOSPACE DIGIT ZERO
U+E0030     '󠀰'     TAG DIGIT ZERO


It's a pity that most glyphs don't show up correctly. That's the extra price you pay for being born in a non-latin script area.

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)

Link to comment
Share on other sites

It's simply that we, occidentals, are soooo used to think only within some kind of ASCII that we too often forget that the vast majority of humans don't use any form of latin scripts, letters, digits and punctuation.

Unicode tries hard (and succeeeds) to include less known (to occidentals) scripts and hence makes the less "common" scripts a tangible reality.

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)

Link to comment
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
 Share

×
×
  • Create New...