Jump to content

Recommended Posts

Hello there,

Here I am once again.

 

I have got a little different problem now.

I tried some preg matching myself, but it didn't workout properly (I am not familiar with them)

 

Here is a string that I got

  2    16  142      0 ^5^1>>^4BLAbla^1<<^0Blaaah^7  50  50 123.12.12.12:-12334    7629  5000

 

Now I need to get the following variables:

$name and $ip

(I don't need any other one's)

 

Now here are the difficulties

1. The amount of white-spaces between the values is random (/s+)

2. The name itself COULD contain (multiple) white-spaces and almost any character (including special ones)

3. The ip port (:number) could be positive or negative (:-number)

 

Here are some constants:

1. Values are always seperated by at least 1 white-space

2. Name always ends with ^7

 

Here is another string (for reference):

5  61  82      jsdJSBCSS72hD67238sDSS 1|^9Bla  hh^113  |*^9Bl aBla^1!        ^7      0 23.233.233.23:22220    2057 25000

 

I hope someone could help me out

Once again I tried my best to make my idea clear  ;)

 

Legenda:

Lime green = always a number

Blue = number or alphanumeric (long or short)

 

EDIT: dang! At the moment I pressed 'submit' I realized that I am (once again) In the wrong forum (should be in php regex forum), SRRY!

 

Link to comment
https://forums.phpfreaks.com/topic/184994-another-template-match/
Share on other sites

Attempt #1

 

$pattern = '#^[0-9]+\s+[0-9]+\s+[0-9]+\s+[a-z0-9]+\s+(.+?)\^7\s+[0-9]+\s+([0-9.:-]+)#i';
$input = '5   61   82      jsdJSBCSS72hD67238sDSS 1|^9Bla  hh^113  |*^9Bl aBla^1!        ^7      0  23.233.233.23:22220     2057 25000';

echo '<pre>';
preg_match($pattern, $input, $out);

It works for the second string.

However it returns an empty array If I try it with the first string...

(I fixed that by putting \s+ in front);

 

And is it also possible to get the first number of the strings?

 

Anyhow, thnx for your help so far!

Your first and second string actually followed different patterns, I guessed as to which one was correct. Your first pattern is ^7 num num ip whereas your second was ^7 num ip. To fetch the first number you just put brackets around the first [0-9]+.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.