Jump to content

stringregexp


iahngy
 Share

Recommended Posts

Hi there,

for stringregexp , what should the pattern be for this string to search for the first plus sign ( some of spaces before + )

$str = " +---"

I tried (?=\s*)[\+]*\b

I remembered it works at begining but then when i turn back to try again it doesnt work anymore...Would you give me a correct pattern.

Link to comment
Share on other sites

Use the autoit tags to post script code

#include <Array.au3>

$str = "      +---"
$aRet = StringRegExp( $str, "(?<= )([+])", 3 );(?=\s*)[\+] or \s\+
_ArrayDisplay( $aRet )

In your case the b is causing the problem because - is not a word boundary

rather you could use s+ as a simplified pattern

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

PhoenixXL, what if i want to search a sub string at the end of a sentence like this below...I wnt to extract 20 to get the number that i need for a calculation ...what the pattern should be?

5.0000nS 10.000nS 15.000nS 20.000nS

Edited by iahngy
Link to comment
Share on other sites

Simple Stepwise approach

Local $String = "5.0000nS 10.000nS 15.000nS 20.000nS"
$String = StringRegExpReplace( $String, '[^\d.\h]', '' ) ;Replace the Non-Digits exculding the periods and space
$String = StringRegExpReplace( $String, '(\.\d+)', '' ) ;Replace the decimal digits

MsgBox(0, 'Formatted String', $String )
;Now two methods
;.....First
Local $iString = StringRegExp( $String, '\d+$', 3 )
$iString = $iString[0]
MsgBox( 64, 'Result', $iString )

;....Second
;Get the last number
MsgBox( 64, "Result", StringRegExpReplace( $String, '([.\d]+\D+)(?!$)', '' ) );Vice-versa of the First Method

Direct Approach

Local $String = "5.0000nS 10.000nS 15.000nS 20.000nS"

;Get the last number
MsgBox( 64, "Result", StringRegExpReplace( $String, '.*[^.\d](\d+).+', '\1' ) );.* - match everything give back as needed
;[^.\d]- match anything that is not a digit or a period such that it is followed by
;(\d+) - [capturing group] at least one digit
;.+ - match everything left over

;\1 - Return only the digits matched
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

;Get the last number
MsgBox( 64, "Result", StringRegExpReplace( $String, '([.\d]+\D+)(?!$)', '' ) )

PhoenixXL,

the code above, (?!$) .....is hard to understand and the whole thing together...does (?!$) mean look for the digit following non digit (space) of which suffix is not the end of the string ...( the end or $ is the number 20 ? )...

Edited by iahngy
Link to comment
Share on other sites

$s = 2.73
$a = stringregexp($s, "\d\.\d\d", 4)
for $i = 0 to ubound($a) - 1
$b = $a[$i]
consolewrite($b[0] &$b[1] &$b[2] &$b[3]
next

PhoenixXL,

Another problem, for ex, I have a float : 2.73

I wnt to split it into 4 parts such as 2, . , 7, 3

if I use stringregexp, what the pattern should be?

mine is d.dd ...and option 4 ...it wont work .

Edited by iahngy
Link to comment
Share on other sites

#include
#include
$s = 2.73
$a = StringRegExp($s, ".", 3)
ConsoleWrite(@error & @CRLF)
ConsoleWrite(_ArrayToString($a, '|') & @CRLF)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Or simply (non-regexp)

#include <array.au3>

$s = 2.73
$a = Stringsplit($s, '',2)
_arraydisplay($a)

Quote from M23, "The thing about SRE's is knowing when to use them".

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

Link to comment
Share on other sites

A $ is an assertion of a EOF

(?!$) - stands for anything not followed by End of File

But i feel as per your simple aim string operations would be a much faster and easier

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Hi,

How can i make a pattern to exclude all chars except the char c or C

for ex,

$s = 'DX990Q_-5C_combo_whatever_uo_abc_8_1x (maybe x is here but sometimes diff char depends on the user)
$pattern = '_[-5910]{1,3}[^\D86]'

I just want to look for -5C or sometimes 95C or 100C or just 5 or 95 or 100 ...not 1x or 8

I put ^D to exclude all chars but how can i add something to get c or C

I think i got it with this simple pattern to look for hot and cold, _-5[cC]?|_100[cC]?

but if 100c or 100C is written 100t, it still take it :(

Edited by iahngy
Link to comment
Share on other sites

Hope this helps

#include <Array.au3>

Local $String = "apartID124_BGA_100C_test1_testfreq_testvolt_ratio32_10x||...-200K//200op?>< 100F"
Local $Match_Array = StringRegExp( $String, "(?i)-?\d+[kfc]", 3 )
_ArrayDisplay( $Match_Array )

Temperatures would most probably be in Kelvin, Fahrenheit or Celsius

This regex will even support negative temperatures with the symbol

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...