Jump to content

Get text between 2 letters in a string


Recommended Posts

  • Moderators

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.

Link to comment
Share on other sites

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
Link to comment
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)
Link to comment
Share on other sites

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
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...