Sign in to follow this  
Followers 0
Dracil

Get text between 2 letters in a string

6 posts in this topic

Hello i need a little help.

Lets say i have a string with "Army Troops : 1000 x"

The number 1000 changes all the time, so how do i grab something by between the ":" and then "x" ?

Thanks in advance

1 person likes this

Share this post


Link to post
Share on other sites



StringSplit for ":" then get rid of anything but the number from the 2nd element.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The search feature ( though not as good as it once was ) still works for simple things:

http://www.autoitscript.com/forum/index.php?app=core&module=search&section=search&do=quick_search&search_app=core&fromsearch=1

Well that link is useless. Doesn't show the query or provide the link to the searched term.

Anyway, if you looked at the "search" feature, typed in "text"+"between" and titles only, this would have been your first popup:

http://www.autoitscript.com/forum/index.php?showtopic=103623&view=findpost&p=733863&hl=%22text%22+%22between%22&fromsearch=1

That one does show the query, but it also has your answer.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Something along these lines would get you close:

; Split string at the ":", return array
$fields = StringSplit ( "Army Troops : 1000 x", ":")

; Remove the right most character (StringTrimRoght) and
; strip white space from the second element in the split
; array (StringStripWS)
$yournumber = StringStripWS(StringTrimRight( $fields[2], 1 ))

... untested but it should be real close.

EDIT: Doh, people answered as I was typing.

Edited by Lee Bussy

Share this post


Link to post
Share on other sites

Here is another four (4) methods for getting the text between two (2) search letters in a string.

#include <String.au3>

Local $sStr = "Army Troops : 1000 x"

Local $iStart = StringInStr($sStr, ":") + 1
ConsoleWrite("StringMid = " & StringMid($sStr, $iStart, StringInStr($sStr, "x") - $iStart) & @CRLF)

Local $aArray = _StringBetween($sStr, ":", "x")
ConsoleWrite("_StringBetween = " & $aArray[0] & @CRLF)

Local $aArrayRE = StringRegExp($sStr, ":(.*)x", 1)
ConsoleWrite("StringRegExp = " & $aArrayRE[0] & @CRLF)

ConsoleWrite("StringRegExpReplace = " & StringRegExpReplace($sStr, ".*:(.*)x.*", "\1") & @CRLF)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Lets say i have a string with "Army Troops : 1000 x"

The number 1000 changes all the time, so how do i grab something by between the ":" and then "x" ?

Here we go with another possible solution ;-)

Local $sStr = "Army Troops : 1000 x"
Local $sResult=StringRegExpReplace($sStr,"(.*?)([0-9]{1,})(.*)","$2")
ConsoleWrite('$sResult = ' & $sResult & @CRLF)

I hope this helps.

Have a nice day.

sahsanu

Edited by sahsanu

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  
Followers 0