Jump to content

[SOLVED] tricky regex?


DamienRoche

Recommended Posts

Here's what I want to do with this.

 

match everything between > and < -

 

1. can have space from beginning or end but cannot be a space and cannot be nothing (i.e. >< or > <) -

 

2. must be either a number or a word - number cannot end with .

 

Here's my current regex with example string:

 

<?php

$str = ">< > < >123.< > 999 < >8,88< >eight< > eig.ht < >ni.ne< > nine <";

preg_match_all('#>.+?<#is', $str, $matches);

print_r($matches);

?>

 

desired output:

 

Array ( [0] => Array ( [0] => > 999 < [1] => >8,88< [2] => >eight< [3] => > eig.ht < [4] => >ni.ne< [5] => > nine < ) )

 

I've had a bash but I can't figure it out. Would love your advice on it. Thanks.

 

Any ideas are welcome.

 

 

Link to comment
Share on other sites

With a flexible approach to a "number" or "word":

 

<pre>
<?php
$str = '>< > < >123.< > 999 < >8,88< >eight< > eig.ht < >ni.ne< > nine <';
preg_match_all('#>\s*(?:[\d,.]+(?<!\.)|[a-z.]+)\s*<#is', $str, $matches);
print_r($matches);
?>
</pre>

Link to comment
Share on other sites

Hi, I've run into a little hiccup with this regex.

 

it ignores anything that is in this format:

 

123.onetwothree

 

it doesn't allow the number and the period in the same string.

 

Is there a way to say only exclude those with . at the end?

 

Thanks.

Link to comment
Share on other sites

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.