Jump to content
Sign in to follow this  
Dracil

Get text between 2 letters in a string

Recommended Posts

Dracil

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

  • Like 1

Share this post


Link to post
Share on other sites
enaiman

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
SmOke_N

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

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.

Share this post


Link to post
Share on other sites
Lee Bussy

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
Malkey

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
sahsanu

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.