Jump to content

How do I filter a string by using Regexp


Recommended Posts

Hi everyone,

 

I am kind of new to Autoit, and right now I am trying to sort out a string and display the way I wanted.

I tried to code with the following code, however, the output result does not seem to be the way I want.

The output shows 100.11 with a whitespace, but I actually want a result like 100.11 9

Please advise how could I display the way I want by using the regexp, I am kind of stuck now.

 

Thank you so much for helping

 

And here is the code:

$strLine = "whatever : whatsoever [100.11 Q9, c, 5]"

$sortString = StringRegExpReplace ($strLine, '[[:alpha:]\s\-\[\]]', "") 
$filterString = StringRegExp ($sortString,'([0-9]]{1,6})', $STR_REGEXPARRAYMATCH,1)

MsgBox(0, "", $filterString)

 

Edited by Mocha
Link to comment
Share on other sites

@Andreik Hi...sorry that I didn't make my question clear because I actually just want to sort the string with all numbers without the letter Q, but I just looked into the help file about the tutorial of StringRegExp, and that's how I learnt from that...but I kind of stuck because I am not familiar and comfortable with the Regular Expression yet.

@Malkey Thank you for helping me with the code, I didn't even think of I could use in this way...thank you so much... I really want to learn well on RegExp, but I am still struggling on that...

Link to comment
Share on other sites

I do not understand your question. Do you have more strings you need to omit letters and then sort them? If so, can you give some examples of other strings? Also, do you want the space in the string? If you do that will affect how your numbers will be sorted.

Edited by abberration
Link to comment
Share on other sites

for sport

$str = "100.11 Q9"
msgbox(0, '' , number($str) & "  " & number(StringReverse($str)))

 

edit: and i think you will find the official forums more useful and engaging than stackoverflow, but that is purely my biased opinion.

Edited by iamtheky

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

Link to comment
Share on other sites

@abberration Hi abberration, yes, actually I have a string like  whatever : whatsoever [100.11 Q9, c, 5] need to filter out to be a format like 100.11 9 only, but I have no idea how to do... and yes, there is a whitespace between 11 and 9 in the string.

@Malkey Hi Malkey, thanks for giving me another answer to work out with :) and I really appreciate for your help. But I find out that if I tried to sort out them and filter them out from a string like whatever : whatsoever [100.11 Q9, c, 5], I'm still not able to do so, so far I have think of the following code below, however I still not getting the answer I want, would you please kindly help me again? Thank you...

$strLine = "whatever : whatsoever [100.11 Q9, c, 5]"

$sortString = StringRegExpReplace ($strLine, '[[:alpha:]\s\-\[\]]', "") 
$filterString = StringRegExp ($sortString,'([0-9]]{1,6})', $STR_REGEXPARRAYMATCH,1)

MsgBox(0, "", $filterString)

@iamtheky Hi iamtheky, thank you for showing your cool way to display and advise XD

Link to comment
Share on other sites

Would this fit the bill?

Local $strLine = "whatever : whatsoever [100.11 Q9, c, 5]"
Local $filterString = StringRegExpReplace($strLine, ".*\[(\d+\.\d+\s)\D*(\d+).*", "$1$2")
MsgBox(0, "", ">" & $filterString & "<")

 

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)

Link to comment
Share on other sites

10 minutes ago, jchd said:

Would this fit the bill?

Local $strLine = "whatever : whatsoever [100.11 Q9, c, 5]"
Local $filterString = StringRegExpReplace($strLine, ".*\[(\d+\.\d+\s)\D*(\d+).*", "$1$2")
MsgBox(0, "", ">" & $filterString & "<")

 

@jchdHey jchd, this perfectly works for me :), thank you for helping! but would you mind please kindly teach me a bit how those syntaxes(pattern) work out in this case? Thank you so much tho...I know I still need to work on how RegExp work...but yeah...still struggling on that...Orz

 

Link to comment
Share on other sites

I highly recommend you use http://regex101.com/ to try patterns and get a detailed explanation about their meaning. Also offers a debugging mode to see how things work step by step.

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)

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