Jump to content

StringRegExp help? How to tell if a string constains a 'code'?


CrewXp
 Share

Recommended Posts

Hey, I don't normally ask for to have code done for me.. but I'm clueless on this one, and to people who know how it works, it should be pretty easy.

How do I check to see if a string contains a 'code'?? Usually we give it out in our twitter profiles, but include additional messages... like so:

Example1: Today's code is DHSHD-SHD24X-BE5SH-SHD55-S5F2J Enjoy guys!

Example2: KRW7C-FHW3T-FXYBM-CQV3R-J3M6? Guess the last digit to win the code!

Example3: Here is the beta code: MRQP4-RXHFM-7B6RF-VC2KV-KC?R6 (Hint: the missing character (the ?) is in the code

Example4: First: CRW2G-JJBTM-89R3Z-X53HK-4JYQ? , Second: BH82P-XJKT7-8J6C2-TTP26-682V?

Example5: Last one. SJDH5-SHDH3-SHDH6-SHFH5-SH__D <- Not sure this will work with the StringRegExp the others will use. Uses _ instead of ?

Example6: V9KVR-6F663-MKVH7-DXQTC-WX9?? Guess the last 2 to win it!

I think StringRegExp is the function I need to be using, but I've never learned how to use it. If I can see an exampe for this particular scenario, I should be able to learn it.

Link to comment
Share on other sites

#include <Array.au3>

Dim $sBlah = 'qpww dwwi wq qq dqdiq wdq dqiw0-i01oekde-okdek-2ok2o-o220w-???02 212 20id q20id 2di 1=di =di 12di12 2di 12=id' & _
            '___ #30ek30k3=230= 3d 3d AAA?1-KMFC?-TRKK1-O9ZAL-?I?IB qqi0-iq-2i0q 2-ei0q2eiq'
            
Dim $aRegEx = StringRegExp($sBlah, '((?:.{5}-){4}.{5})', 3)
If IsArray($aRegEx) Then _ArrayDisplay($aRegEx)

; or with word-bounding:

$aRegEx = StringRegExp($sBlah, '\b((?:.{5}-){4}.{5})\b', 3)
If IsArray($aRegEx) Then _ArrayDisplay($aRegEx)

?

Link to comment
Share on other sites

quick question. If the RegExp includes Word bounding, that means that it only searches if the 'code' is separated by spaces? Right?

First example returns 2 codes, second example returns the code that is separated by spaces

All characters in the " \w " set are a-z, A-Z, 0-9 and underscore (_). They are called word characters.

All characters NOT in the " \w " set are in the " \W " set which is called the the non-word characters.

" \b " matches at a word boundary. This is where the classification of the characters change from one set of characters to the other set of characters based on the word, non- word categories or definition.

The space is in the non-word category. If the next character after the space is in the word category, a split occurs due the the non-word , word boundary.

The example shows the splite at word - non-word boundary of various word and non-word characters. Notice when there is a space separating non-word characters no split occurs.

#include <Array.au3>
    
  ; From  http://www.autoitscript.com/autoit3/pcrepattern.html
  ;A word boundary is a position in the subject string where the current character and the previous 
  ;character do not both match \w or \W (i.e. one matches \w and the other matches \W), or the start 
  ;or end of the string if the first or last character matches \w, respectively. 
    
  ; From Help File under StringRegExp 
  ;\w Match any "word" character: a-z, A-Z, 0-9 or underscore (_). 
  ;\W Match any non-word character 
    
    
    Local $sString = " Jan%@& /\?Z$ #  ><Ab C12_)([]{}5!^$1"
    
    Local $aREResult = StringRegExp($sString,"(\b.+?\b)",3)
    
    If IsArray($aREResult) Then _ArrayDisplay($aREResult)

Edit: Last line of script added

Edit No2.

CrewXp

In 1st post, example1, one of the capture sections of the code has six characters between the dashes.

This means if you want Authenticity 's solution to work on that example 1, you will need to substitute {5} with {5,6} in the regular expression.

Edited by Malkey
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...