Regex not working as expected [duplicate] - php

This question already has answers here:
What regex to use for this
(6 answers)
Closed 4 years ago.
I'm having troube modifying this regex. Right now it matches . or ? but I want to change it to match dot followed by a space. How do I do that?
'('/([.|?])/'
By the way, I need the grouping to stay.

What about this:
(\. |\?)
......

The easiest way would be:
'('/(\. )/'
or, if you want a space or a tab or a new-line:
'('/(\.\s)/'
Note that I only changed the part in the inner parenthesis as that part seems to be the focus of your question.

/\.\s/ should work for matching a dot followed by a space..
note: \s matches any whitespace

Related

regex does not match if string contains spaces [duplicate]

This question already has an answer here:
Optional Whitespace Regex
(1 answer)
Closed 2 years ago.
I'm using regex js\w*=\"[^\"]+\" to match and replace tags in HTML that begin with js like jsname="name" jscontroller="somecontroller" etc.
Problem is, if there are spaces before and after the = sign it matches nothing. How can this be solved?
This matches fine jsname="name" jscontroller="somecontroller"
This does not match but should: jsname = "name" jscontroller = "somecontroller"
Can you help?
Use js\w*\s*=\s*\"[^\"]+\"

regex to only match a full set of two-part pattern (kinda non-greedy) [duplicate]

This question already has answers here:
Non-greedy string regular expression matching
(2 answers)
Regex lazy quantifier behave greedy
(2 answers)
Closed 3 years ago.
I use preg_match_all to get all matches of a two-part pattern as:
/<(.*?)>[:|\s]+{(.*?)}/
In a string like:
<First>: something <second>: {Second} something <Third>: {Third}
I want to match:
<second>: {Second}
instead of:
<First>: something <second>: {Second}
Working Example
Where did I do wrong?
Use limited repeated set instead of lazy repetition inside the brackets:
<([^>]*)>[:\s]+{(.*?)}
The change is to replace <(.*?)> with <([^>]*)>. The initial version matches the first < then takes lazily any character until it finds :{Second}. If you restrict repetition, regex engine will try to start with <First>, but when it doesn't find :{...} after that, it'll try with the next <
Demo

Regex PHP part of string [duplicate]

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

Regex in PHP: How to get word in last part URL? [duplicate]

This question already has answers here:
Regex in Notepad++ : How to get last part of URLs?
(4 answers)
Closed 8 years ago.
I have some lines like this:
/gtrhr/5435-silent-library
/fghf3/435-silent-librar
/gfgr2gf/8768-silent-lib
How to get ONLY words in last part of URL ?
So, the oupout will be:
silent-library
silent-librar
silent-lib
Thank you !
[A-Za-z][A-Za-z\-]+$
The $ enforces the match ends with the end of the line.
See here on Regexr.
You could also go for a more general [a-z][\w\-]+$ but that depends on precisely which characters are valid or not in your inputs.

Preg_replace with multiple lines(php) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace text over multiple lines using preg_replace
I am trying to read some text from a file and than replacing some pattern. If I try to replace the patern from just a single string it works, but if there are multiple such strings in a file it doesnt work.
$this->session->set_flashdata('error_message', 'Naslovna vrstica je bila uspešno shranjena');
This is an example of text that I am trying to replace the replacment works ok with just this line, but not if there are other such lines in a file, which all match individualy though.
$content = file_get_contents("C:\Users\Borut\\test.txt");
$pattern="/^.*session->set_flashdata\((.*),(.*)\);$/";
$replacement="\$_SESSION[$1]=$2";
this is my code. How do you replace multiple strings as the one shown above.
The modifier you want is m. You can find all modifiers here
That said, the easiest and better regex solution would be
"/\$this->session->set_flashdata\((.*?),\s*(.*?)\);/"
Notice how there's a ? after the .* in each. This is to stop greedy matching as with yours. Also notice that the modifier isn't required either with the removal of the ^ and $

Categories