Jump to content
Mocha

How do I filter a string by using Regexp

Recommended Posts

Mocha

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

Share this post


Link to post
Share on other sites
Andreik

Your description is vague. Is this all your script has to do? This filter has to be apply to other strings? If yes how in whici way these strings are different?

  • Like 1

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Malkey

This filters "100.11 Q9" to "100.11 9" by removing all ASCII letters.

$strLine = "100.11 Q9"
$sortString = StringRegExpReplace($strLine, '[[:alpha:]]', "") ; Removes all ASCII letters.
MsgBox(0, "", $sortString, 2)

 

  • Thanks 1

Share this post


Link to post
Share on other sites
Mocha

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

Share this post


Link to post
Share on other sites
abberration

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
  • Like 1

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites
iamtheky

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
  • Thanks 1

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

Share this post


Link to post
Share on other sites
Mocha

@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

Share this post


Link to post
Share on other sites
jchd

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 & "<")

 

  • Thanks 1

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)

Share this post


Link to post
Share on other sites
Mocha
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

 

Share this post


Link to post
Share on other sites
jchd

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.

  • Like 1
  • Thanks 1

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)

Share this post


Link to post
Share on other sites
Mocha

thank you for offering the help jchd! I will definitely go to try it out this tool! Thank you so much!!

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

×

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.