/    Sign up×
Community /Pin to ProfileBookmark

again regular expression question

Hello guys.

I need a little help with regular expression again.

[B]Problem:[/B]
I have a string containing $ on several places in it. The string is actualy a text file loaded, so any word starting in the text file with $ is beng considered as a text not variable.

[B]Question:[/B]
How to obtain the list of all the words which start with $ in the string.

e.g.

[B]The text file contents are:[/B]

[COLOR=Teal]
My name is $thename
My age is $theage[/COLOR]

[B]PHP script loading the file:[/B]
[COLOR=Blue]$myvar = implode(”,file(‘myfile.txt’));[/COLOR]

And what I need is:
[COLOR=DarkRed]$list = list_dollar_words($myvar);[/COLOR]
[COLOR=DarkSlateGray]//which must return an array of words starting with $ sign.
//means it must return array of two elements when having data of above
//text file:
//Array( [0] => $thename, [1] => $theage)[/COLOR]

I hope I explained enough.

will be waiting for kind reply.

regards

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 27.2006 — Maybe this: /b($w+)b/
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — No, I mean which function to use ? and pass this, what will be syntax ?

consider me totally blind when it comes to regex lol.

waiting for reply.

regards
Copy linkTweet thisAlerts:
@bokehNov 27.2006 — [code=php]<?php

$file = 'myfile.txt';

$list = array();
if(preg_match_all('/$[^s]+/', file_get_contents($file), $matches)
{
$list = $matches[0];
}

?>[/code]
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — great but,

one more thing, if text contains array it must not get all of its indexes.

e.g.

$settings[abc] and $settings[xyz], must result in only $settings putin the array not rest of its part, just following the php variable definition, actually I need the lsit of variables in a text based template.


regards
Copy linkTweet thisAlerts:
@bokehNov 27.2006 — Post a string and mark in bold what you want to capture.
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — sure

"My name is [B]$thename[/B]<br> my age is [B]$theage[/B]/";

using the last script you provided, I get array

Array( '$thename<br>', '$theage/')

Where, I need the words containing a-z,A-Z,0-9, and _ (underscore), rest of the portion of the word must be discarded.


I hope I could explain.

regards
Copy linkTweet thisAlerts:
@bokehNov 27.2006 — Use the expression in post #2 without the parenthesis along with the code I posted in post #4.
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — Extremely sorry to bug you.

I cant follow which parenthesis you are pointing to.

can you write exact expression for me please ?

regards
Copy linkTweet thisAlerts:
@so_is_thisNov 27.2006 — He's saying to use the code he posted but to substitute this Regular Expression:
Maybe this: /b($w+)b/[/QUOTE]
But without the parentheses. Thus, as follows:

/b$w+b/
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — In following code is function returns false I think and script prints 'No matches'

Can you kindly let me know where am I making mistake ?
<i>
</i> $data = 'My name is $thename&lt;br&gt; my age is $theage/';

<i> </i>$list = array();
<i> </i>if( preg_match_all('/b$w+b/', $data, $matches) )
<i> </i>{
<i> </i> $list = $matches[0];
<i> </i> print_r($list);
<i> </i>} else
<i> </i> echo "No matches";


regards
Copy linkTweet thisAlerts:
@bokehNov 27.2006 — [code=php]if( preg_match_all('/$w+/', $data, $matches) )[/code]
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — Thanks alot, it worked, b were extra.

regards
Copy linkTweet thisAlerts:
@so_is_thisNov 27.2006 — They're not really extra. They are for the purpose of insuring word boundaries. In a perfect world, and with perfect data, you can get along without them, but... However, if you're happy with the results, who am I to question them. ?
Copy linkTweet thisAlerts:
@Ali_ImranauthorNov 27.2006 — I actually wanted to say, they are extra in the expression I needed, I do not know either they are too important. I am regular developer of php mysql but when it comes to regex am layman.

you too, thanks alot ?
Copy linkTweet thisAlerts:
@bokehNov 27.2006 — They are for the purpose of insuring word boundaries.[/QUOTE]The reason the expression didn't work is because the space and the dollar sign are both non-word-characters hence not a word boundary.
Copy linkTweet thisAlerts:
@so_is_thisNov 27.2006 — Ah, yes, good point. I overlooked the fact that I coded the $-sign inside the word boundaries. Good catch.
×

Success!

Help @Ali_Imran spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...