Jump to content

[SOLVED] Catch paranthesis


ballouta

Recommended Posts

Hello

I have a string on this format:

 

sentence one goes here (1) sentence two goes here (2) etc...

I am looking for an expression inorder to split each sentence so I can insert it in a field in a DB separately,,

 

the output should look like:

sentence one goes here

sentence two goes here

(I will put the insertion code here for each sentence)

 

Thanks in advance

Link to comment
Share on other sites

I think it would help to have an actual sample string to examine.. Using preg_match_all, one would need to know where to break the sentence up into captured chunks. One of the big problem sacross these forums is poor initial communication. Some people will list a bunch of sentences stacked ontop of one another (which could leads others to think about arrays, only to find out it was all in one long string). Being clear and showing examples from the get-go is certainly a faster way of getting some help as opposed to half-assed explanations which cause wasted time and confusion.

 

On the topic of getting help, I think it wouldn't hurt to look up beginner tutorials on regex on the net. While getting the fast and easy answers does eliminate any work on your end, it doesn't further improve yourself. Even a basic knowledge of stuff on regex can go a long way in setting up the foundation to answers you need. After all, the more you know, the less dependant you will be on others.

 

It's just a suggestion. I help out if I can (or feel like it) for the practise sake of it.. otherwise, I would leave these people with the issues they have.

Link to comment
Share on other sites

I think what ballouta wants is to separate a string into chunks using ([0-9]*) as the separator.  It's really not that hard.

 

All you need to know is how regex works and how preg_match_all stores back references.  There are tons of regex sites with helpful examples.  Learn to use google.  The php docs are one the best online docs for a programming language - http://us2.php.net/preg_match_all

 

There's a difference between asking for programming help, and hoping someone will just do it for free.  I can't speak for everyone here, but I'm not going to just give you a full answer.  If you show some work, ask nicely, and have showed that you've really tried and are stuck, then I would be more than glad to help.  If you're not willing to learn, or show any sign of effort, then hire a programmer.

 

On a related note, I'm available for hire.

Link to comment
Share on other sites

I was thinking along the lines of:

preg_match_all('#([-a-z\s.?!])+#i', $str, $matches); // allow for extra characters in the sentences
//then echo out $matches[0] in foreach loop

 

It would work (on condition that the sentences don't contain any characters not listed in the character class)..But admittedly not nearly as flexible nor streamlined as your solution.

 

On a side note, I picked up 'Mastering Regular Expressions' yesterday and am going through it chapter by chapter.. only on chapter 2 and already some things are getting clearer.. so this book looks very promising. By the end of it, I should be far more knowledgable with regex.  :-\

Link to comment
Share on other sites

Note: the sentences in my situation will never contain the characters ) or ( so don't worry

 

Ah.. well, that changes things.. had your sentences had (1), (2) etc.. effigy's method would be the best one.

This is why I ask people to give an ACTUAL example of what they have to work with.. given what you mentioned, your example:

 

sentence one goes here (1) sentence two goes here (2) etc...

 

is not correct as it has ( and ) characters in it.

When it boils down to examples to be used in regular expressions, it is very important to have crsytal clear examples and an equally clear goal desciption..simply because regular expressions is such a strict 'mini-language', there is no room for errors.

Link to comment
Share on other sites

I prefer the preg written by nrg_alpha since the output is exactly as I want

 

It only covers -, a-z, white space, ., ?, and !. Is that enough? No one is going to throw in @, #, $, %, &, *, etc? The parens are not needed: #[-a-z\s.?!]+#i.

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.