Jump to content
MrCheese

stringregexpreplace question

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!

 

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

@mikell
Thanks for the comments! 

I'm sure that your comments help a lots who wants to learn more about SRE and SRER :)
Appreciate that! 
:huggles:


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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 : HERE ( 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

 

Share this post


Link to post
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

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

Share this post


Link to post
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:]]

Share this post


Link to post
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)

Share this post


Link to post
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)

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

  • Similar Content

    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
    • By junichironakashima
      Im creating a code that will work in this sequence:
      1. Copy the text (question) in one atea of the screen
      2. Catch the 2 strings (number)
      3. Multiply the 2 strings ( $1*$2)
      4. Click the next area to put the answer
      5. Paste the answer
       
      This is my code
       
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c") 
      $x = StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1)
      ClipPut($x)
      Send("^v")
       
      However the output is this
      $1*$2
       
      How can I make it solve itself? Because I tried this code:
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1) $x = Execute(StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2"))
      ClipPut($x)
      Send("^v")
      Output is just blank text

    • By mLipok
      In April 5, 2013 I ask @Lazycat 
      he answer:
      Then I change this tool a little.
      Now I back to this and make bigger changed.
      Here is new version.
      Update History: = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/07 v3.0 * Changed: AU3Check compilant - mLipok * Changed: almost all Variables renamed - mLipok * Added: "Delete RegExp Results" - mLipok * Added: support for dual monitor - mLipok * Added: "full screen mode" - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/08 v3.1 * Added: colors for each Edit control - used GUICtrlSetBkColor() - mLipok * Added: FullScreen option (Checkbox + INI + Remarks in Tip) - mLipok * Added: _IsChecked() - mLipok * Changed: WinMove() - change size of window using: WindowWidth and WindowHeight - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/13 v3.2 * Added: If $bFullScreen Then GUICtrlSetFont() - mLipok * Added: WM_COMMAND , $EN_CHANGE - prevent CPU overheat - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2018/11/29 v3.3 * Changed: $_g_idCheckbox_Clear - also clear $_g_idEdit_Result - mLipok * Changed: ClearResult If GUICtrlRead($_g_idEdit_MatchText) = '' Or GUICtrlRead($_g_idEdit_MatchText) = '' - mLipok * Fixed: prevention CPU overheat - If $iGuiMsg <> 0 Then $_g_bWasAChange = True - any GUI change will fire RegExp result refresh - mLipok * Fixed: Top possition of $_g_idLabel_Dummy control - mLipok * Added: support for TabSwitch - CTRL+TAB and CTRL+SHIFT+TAB - mLipok = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
    • By PClough
      Hi everyone!
      After updating autoit, I tried to run an old program using complex regexp's.  It did not work.  Eventually I broke the problem down to this example:
       
      #include <Array.au3> $buf = "First title" & @CRLF & "Tom" & Chr(0x92) & "s sleepwalking" & @CRLF & "Last | line" & @CRLF $items = StringRegExp($buf, '([\x20-\xff]+)\x0d\x0a', 3) _ArrayDisplay($items,'') And this is the result I get when running it:
      Row 0
       
×
×
  • Create New...