0

I would like to get a string made of one word with a delimiter word before and after it

i tried but doen t work

$stringData2 = file_get_contents('testtext3.txt');
$regular2=('/(?<=first del)*MAIN WORD(?=last del)*\s');      

preg_match_all($regular2,                          
$stringData2,
$out, PREG_PATTERN_ORDER);

thank you very much for any help

5
  • Can you give me an example of what you would have in testtext3.txt? Commented Mar 6, 2014 at 16:49
  • the text inside test text3 is a long text with many words equal to the delimiters so i have to restrain it just from the main word to the delimiters before and aftre it Commented Mar 6, 2014 at 17:08
  • That's fine, but a small example of what would be in the text file and what you would want the regular expression to return in that instance, would really help us to give you the answer you need. Assuming none of the answers already given help you. Commented Mar 6, 2014 at 17:13
  • DELI STR text text text MAIN WORD text text tetx DELIend str texttexttexttexttetxt DELI STR texttexttexttexttexttext DELI STR textetxtetxttexttexttexttext DEL STR text text text text text text MAIN WORD text text text tetx DELIMend STR etc....... Commented Mar 6, 2014 at 17:30
  • Just the strings between DEL STR ....MAIN WORD.....DELIend STR only with the delimiter string included Commented Mar 6, 2014 at 17:34

3 Answers 3

2

No quantifier needed, add delimeter at end, put \s inside lookahead.

'/(?<=first del)MAIN WORD(?=last del\s)/'
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, still editing before your comment showed up on my machine. This is the energizer bunny regex error, it keeps going and going..
Good call, sln. Maybe make the whitespace optional? i.e. \s? That way if it's at the end of the file, it'll still match. Could put optional whitespace in the lookbehind, as well.
@ PhilipBennison - I just fixed his regex, didn't want to modify it. But, in the lookahead the \s might be required. Adding a \s? would cause a problem. If its an EOF possibility, its more appropriate to do this (?:\s|$). In the lookbehind, adding \s? would make it variable length. If its optional there, it could be (?:(?<=^first del)|(?<=\sfirst del))
0

This regex

(?<=xx)[^\s]*(?=yy)

matches hello in:

xxhelloyy

but fails to match in:

xxhello worldyy

This is probably what you're looking for.

1 Comment

I would like to get xxhelloyy because the string is taken from a long text and if i don t do anything i get all the text between the first delimiter and the main word. I just want the text from the main word starting form the first delimiter and ending with the last delimiter
0

If you want the delimiter string included in the match, then you should not be using lookahead or look or look behind. It should be something rather basic, like this.

/\s?first del MAIN WORD last del\s?/

If you do want to return JUST the MAIN WORD part of the match, then this will work.

/(?<=\s?first del)MAIN WORD(?=last del\s?)/

Put a 'i' at the very end of that to make it case insensitive, if you want. I only mention this, because in the example you gave me above has different case between the example text and the desired response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.