UTF 8 in preg_match - php

sorry for my English.
I’m trying to use preg_match with utf-8 in PHP.
preg_match("/\bjaunā\b Iel.*/iu", "Jaunā Iela");
Function returns 0. But
preg_match("/\bjauna\b Iel.*/iu", "Jauna Iela");
works fine.
Why?
Thanks.

Word boundaries don't work correctly with special chars. In the text Jaunā Iela the word bounderies are: \bJaun\bā \bIela\b
So instead of using word bounderies, try a look-ahead and look-behind assertion for a space. (or beginning of string) Like so:
The regex:
(?<=^|\s)Jaunā(?=\s) Iel.*
PHP:
preg_match("/(?<=^|\s)Jaunā(?=\s) Iel.*/i", "Jaunā Iela");
Working regex example:
http://regex101.com/r/tV6yR9

Related

NetBeans replace word with another word from the text

I'm new to PHP coding and I made a mistake in the code.
I have like 400 occurrences of a method that I want to change but I don't know how.
I've heard about regular expressions, but I can't apply to this case because I dont know so much of RegExp.
I have this in my code, for example
<?php echo $lang['extension_not_allowed'] ?>
I want to change all the results with $lang for $this->lang(''), for example
<?php echo $this->lang('extension_not_allowed') ?>
There's any way to do it using Ctrl+Shift+H (Global Replace) in NetBeans?
Thanks.
You can use the following regex with replacement:
(<\?php.*?)\$lang\[([^]]*)]
And the replacement:
$1\$this->\$lang($2)
Or remove the (<\?php.*?) part if the $lang may appear on lines without <?php:
\$lang\[([^]]*)]
and replace with "\$this->\$lang($1)".
See demo
A couple of notes:
(<\?php.*?) - matches and captures the text <?php and 0 or more characters other than a newline, as few as possible (with .*?)
\$lang\[ - matches $lang[ literally (note that special regex characters must be escaped)
([^]]*) - matches and captures 0 or more characters other than a ] (we are using a character class [...])
] - a literal ].

regex difference with php whitespace

I can't get this regex to work with PHP specifically the whitespace in the middle, the value or unit match group individually will match.
regex:
/(?<value>\d+\.?\d*)(\p{Z}|\s)(?<unit>(meters|mm))/
string to parse:
Cord Length:1.52 meters
try on http://www.phpliveregex.com/ it doesn't match.
http://www.phpliveregex.com/p/bV7
try on https://regex101.com/ it works fine
EDIT: still doesn't seem to be working on phpliveregex.com for me
http://www.phpliveregex.com/p/bV7
EDIT2: I have edited the string to parse.
Apart from the above comments, I would modify your regular expression as follows:
(?<value>\d+(?:\.\d+)?)\h+(?<unit>(?:meters|mm))

simple regex for italian words

I can't understand a simple regex that I need for a control over a preg_match statment:
-I need that every words with even white space between, and accent and apostrophe are allowed
so something like: sjsjsjjsjsjs òòòò èèèèè ddddd ''' eerfk jefrkj sdc should be accepted
so i write something like: [a-zA-Z\xE0\xE8\xE9\xF9\xF2\xEC\x27\s]*
that take everything that is letters and some special HEX code for accent and apostrophe, but i can't understand how to concatenate the sentence:
[^\r\n]
I'd like to reject anything if there is an end of line or a return statement. The puntaction too but it seem to be allreafy solved with my regex
so something like:
adjnasdnjadsija adokasmdoasdmoa admoadsoasodoas END
sddaadsasd òòò
should be accepted until the words END
Is it the right code? i made several test but no result!
I test my regex over http://regex101.com/
Set the locale appropriately:
setlocale(LC_ALL,"it_IT");
Now you can use a much simpler regex:
/[\w\s]*/
This is because \w is locale-aware ^_^

php preg_match url regex not working

I can't get this function working correctly:
function isValidURL($url){
return preg_match('%http://domain\.com/([A-Za-z0-9.-_]+)/([A-Za-z0-9.-_]+)%', $url);
}
The url:
http://domain.com/anything-12/anything-12/
can contain numbers, letters and symbols _ -
I assume its to do with the first regex - as these work
http://domain.com/anything12/anything12/
http://domain.com/anything12/anything-12/
http://domain.com/anything12/any-thing-12/
http://domain.com/anything_12/any-thing-12/
As always all help is appreciated and thanks in advance.
You need to escape the - in the character class of your regex.
You need to anchor your regex so that tries to match the entire input string and not part of it.
The modified regex is:
'%^http://domain\.com/([A-Za-z0-9.\-_]+)/([A-Za-z0-9.\-_]+)/$%'
You can shorten your regex by noting that [A-Za-z0-9_] is same as \w and also there is a repeating sub-regex.
'%^http://domain\.com(/[\w.-]+){2}/$%'

PHP preg_replace non-greedy trouble

I've been using the following site to test a PHP regex so I don't have to constantly upload:
http://www.spaweditor.com/scripts/regex/index.php
I'm using the following regex:
/(.*?)\.{3}/
on the following string (replacing with nothing):
Non-important data...important data...more important data
and preg_replace is returning:
more important data
yet I expect it to return:
important data...more important data
I thought the ? is the non-greedy modifier. What's going on here?
Your non-greedy modifier is working as expected. But preg_match replaces all occurences of the the (non-greedy) match with the replacement text ("" in your case). If you want only the first one replaced, you could pass 1 as the optional 4th argument (limit) to preg_replace function (PHP docs for preg_replace). On the website you linked, this can be accomplished by typing 1 into the text input between the word "Flags" and the word "limit".
just an actual example of #Asaph solution. In this example ou don't need non-greediness because you can specify a count.
replace just the first occurrence of # in a line with a marker
$line=preg_replace('/#/','zzzzxxxzzz',$line,1);

Categories