Jump to content

Filling in the middle empty spaces StringRegExpReplace?


Recommended Posts

I want to fill the gap and merge 127.0.0.1 8080

127.0.0.1:8080

$YourIPProx = '<div class="' & @CRLF & _
        'blablablabla Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
        '</div>'
$regex = StringRegExp($YourIPProx, '((?:\d{1,3}\.){3}\d{1,3})\s\d{1,4}', 3)

If IsArray($regex) Then
    $IPProxy = StringRegExpReplace($regex[0], '((\d{1,3}\.){3}\d{1,3})\s:\d{1,5}', "$1")
    MsgBox(0, "", $IPProxy)
EndIf

 

Link to comment
Share on other sites

@youtuber

Try this pattern directly with with your test string (<div>...</div>) in StringRegExpReplace() function:

; Test Pattern:
'(?s)(?:.*?)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s(\d+)(?:.*)'
; Replace Pattern:
'$1:$2'

With this pattern and replace strings, you can do what you're trying to do in one shot :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

4 hours ago, youtuber said:

I want to fill the gap and merge 127.0.0.1 8080

Your code was correct (one closing parenthesis to move only)

$YourIPProx = '<div class="' & @CRLF & _
        'blablablabla Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
        '</div>'
$regex = StringRegExp($YourIPProx, '((?:\d{1,3}\.){3}\d{1,3}\s\d{1,4})', 3)
; MsgBox(0, "", $regex[0])

If IsArray($regex) Then
    $IPProxy = StringReplace($regex[0], " ", ":")
    MsgBox(0, "", $IPProxy)
EndIf

 

Link to comment
Share on other sites

Why don't you do something that is simpler and shorter?

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Global $strString = '<div class="' & @CRLF & _
                    'blablablabla Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
                    '</div>'

MsgBox($MB_ICONINFORMATION, '', "Before: " & $strString & @CRLF & @CRLF & _
                                "After : " & StringRegExpReplace($strString, '(?s)(?:[^\d]*)((?:\d{1,3}\.?){4})\s(\d+)(?:.*)', '$1:$2'))

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

3 hours ago, FrancescoDiMuro said:

Why don't you do something that is simpler and shorter?

Because youtuber posts a lot about regex, so I assume that he wants to learn regex, and it is a way to show that his own pattern is nearly good
Of course there are many ways to skin this cat  :)

Edit
BTW  you might test your pattern against these strings

Global $strString = '<div class="' & @CRLF & _
                    'blablablabla5 Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
                    '</div>'
                    
Global $strString = '<div class="' & @CRLF & _
                    'blablablabla Your IP Proxy blabla 5555 127.0.0.1 8080 123A123' & @CRLF & _
                    '</div>'

 

Edited by mikell
Link to comment
Share on other sites

@mikell
First rule of a RegEx user: "Know your string".
Since there were no \d before the wanted ones, I used [^\d]; indeed, a .*? can be used to avoid such problems:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>


Global $strString = '<div class="' & @CRLF & _
                    'blablablabla5 Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
                    '</div>'

MsgBox($MB_ICONINFORMATION, '', "Before: " & $strString & @CRLF & @CRLF & _
                                "After : " & StringRegExpReplace($strString, '(?s)(?:.*?)((?:\d{1,3}\.?){4})\s(\d+)(?:.*)', '$1:$2'))

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@mikell

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>


Global $strString = '<div class="' & @CRLF & _
                    'bla1234 5blablablabla Your IP Proxy blabla 127.0.0.1 8080 123A123' & @CRLF & _
                    '<\div>'

MsgBox($MB_ICONINFORMATION, '', "Before: " & $strString & @CRLF & @CRLF & _
                                "After : " & StringRegExpReplace($strString, '(?s)(?:.*?)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s(\d+)(?:.*)', '$1:$2'))

;)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

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