V
V
Valenso2012-05-09 17:34:43
Regular Expressions
Valenso, 2012-05-09 17:34:43

How to remember all occurrences?

There is an integer string
gg =665 jh = 78;
Check it for a condition and remember the symbols gg, j, h.
I wrote a regular expression:

/^\s*integer\s+(?:\s*([az]+)\s*(?:=\s*[0-9]+\s*){0,1})+ ;$/ I get Array

from it ( [0] => Array ( [0] => integer gg =665 jh = 78; ) [1] => Array ( [0] => h ) ) It turned out that I remembered only the last entry, I want to catch everyone else too.


Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
danmiru, 2012-05-10
@danmiru

Will it fit?
([a-z]+)[ =]*([0-9]*)
preg_match_all('/([a-z]+)[ =]*([0-9]*)/i', 'целое gg =665 j h = 78;', $result);
Array
(
[0] => Array
(
[0] => gg =665
[1] => j
[2] => h = 78
)
[1] => Array
(
[0] => gg
[1] => j
[2] => h
)
[2] => Array
(
[0] => 665
[1] =>
[2] => 78
)
)

V
Vasiliy Shilov, 2012-05-09
@web36m

You group (? :), but you have to remember
. So it won't work?
/^\s*integer\s*([az]+)\s*\=[0-9]+\s*([az]+)\s*([az]+)\s*\=\ s*[0-9]+;$/

V
Vasiliy Shilov, 2012-05-09
@web36m

/^\s*integer\s+(?:\s*([az]+)\s*|([az]+)\s*\=\s*[0-9]+)+;$/
gets j and h sorry no time, would have finished it

W
Wott, 2012-05-09
@Wott

Here, the features of the work of regular expressions pop up
1. the repeated occurrence is checked from the place where the previous one ended, so you should not wait for several results from the regular expression for the entire line - one result will be found, the last one.
2. to get all occurrences, you need to match the entire regular expression
. From this we get that for (var=123) + you need to write one and to check for "integer" - the other.

D
dsd_corp, 2012-05-09
@dsd_corp

Your condition is of course very vague.
TK is clearly understandable only to you)
From what I understand, for PHP:

$s="Есть строка\n".
"целое gg =665 j h = 78;".
"Проверить ее на условие и запомнить символы gg, j, h.".
"целое z s=234 f r = 35 s".
"целое a b c d e".
"целое i=23 j=13 k=4567 l=13243546 m=0".
"Написал регулярку:";

$r=preg_match_all('/целое(?:[\s]+[a-z]+(?:[\s]*?\=[\s]*?[\d]+)?)+/is', $s, $matches, PREG_SET_ORDER, 0);
if ($r==false || $r<=0) exit(0);
$result=array();
foreach ($matches as $m) {
  $r=preg_match_all('/[\s]+([a-z]+)(?:[\s]*?\=[\s]*?[\d]+)?/is', $m[0], $rmatches, PREG_SET_ORDER, 0);
  if ($r==false || $r<=0) continue;
  $res=array();
  foreach ($rmatches as $rs) {
    $res[]=$rs[1];
  }
  $result[]=$res;
}
@file_put_contents('out_html.txt', var_export(array($matches, $result), true));
exit(0);

Either way, you need two passes. On the first one we select strings by format, on the second we get all the necessary attributes.

M
Mikhail Osher, 2012-05-10
@miraage

On your question, I thought the following.
The line "integer gg =665 jh = 78;", but let's look at it differently:
"bullshit remember bullshit remember remember bullshit bullshit;"
Do explode by space and take away the necessary indices =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question