/    Sign up×
Community /Pin to ProfileBookmark

Tiny Regex Problem

Hi Guys,

I get too lost when writing out regular expressions so could someone give me a little help please.

I want to search a string of a certain pattern, if it is the right pattern then extract the variable from the certain position.

This is the basic format of the string:

[CODE]{COLUMN VARIABLE}(Linked:{TABLE VARIABLE}[/CODE]

An example would be:

[code=php]$string=’area(Linked:areas)’;[/code]

The variable I want to extract is the ‘TABLE VARIABLE’, so in this example it would be ‘areas’

Once I have saved the variable I then want to use preg_replace to remove the brackets and contents so the end result will be something like this:

[code=php]
$STRING=’area(Linked:areas)’;
// Do some regex work here to extract table variable and delete brackets and contents
$COLUMN=’area’;
$TABLE=’areas’;
[/code]

Any help much appreciated,

Ben

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Benji6996authorDec 10.2010 — All sorted I think. Will post my solution in a min once I have finished. Let me know if it can be improved.
Copy linkTweet thisAlerts:
@Benji6996authorDec 10.2010 — [code=php]foreach($COLUMNS as $key => $value){
preg_match('/:(.+))/', $value, $matches);
if($matches[1]){
$COLUMNS[$key]=preg_replace('/(.+/', '', $value);
$DBLINKS[$COLUMNS[$key]]=$matches[1];
}
}[/code]
Copy linkTweet thisAlerts:
@thraddashDec 10.2010 — This produces the initial requirement...

[code=php]$STRING = 'area(Linked:areas)';

if (preg_match('/^([^(]+)([^:]*:([^)]+)/', $STRING, $matches)) {
$COLUMN = $matches[1];
$TABLE = $matches[2];
echo 'Column: ' . $COLUMN . ' Table: ' . $TABLE;
}[/code]
Copy linkTweet thisAlerts:
@Benji6996authorDec 11.2010 — much better, thank you!
×

Success!

Help @Benji6996 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 5.10,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...