regex

How to insert the whole matched text in the replacement in Perl?

牧云@^-^@ 提交于 2021-02-05 12:20:03
问题 I want to add a string ( A ) after all specific other strings ( bbc ). So, I match bbc and want to replace it with itself with A appended ( 'aabbcc' => 'aabbcAc' ). Is there a replacement back-reference that gets substituted with the whole match? $0 doesn't seem to work – its content is always "-e", for some reason: $ echo 'aabbcc' | perl -p -e 's/bbc/$0A/g' aa-eAc 回答1: Use $& , see http://perldoc.perl.org/perlvar.html echo 'aabbcc' | perl -p -e 's/bbc/$&A/g' aabbcAc 来源: https://stackoverflow

Why preg_match(“/[^(22|75)]/”, “25”) returns false?

被刻印的时光 ゝ 提交于 2021-02-05 12:19:30
问题 I want to test that a given string does not belong to the following group of strings: 22 75. Could anyone please tell why PHP's preg_match("/[^(22|75)]/", "25") returns 0 ? The weirdest thing is that preg_match("/[^(22|76)]/", "25") returns 1 as expected... Edit: I guess I understand the reason and the nature of my mistake, not how to make a check that a given two-digit number does not match 20,21,22,23,24, 75,76,77,78,79,80 ? I need to assemble an expression to check a given age against the

Match regular expression containing multiple dots [duplicate]

我是研究僧i 提交于 2021-02-05 12:18:53
问题 This question already has answers here : Match text between two strings with regular expression (2 answers) Closed yesterday . In Python 3.8, I am trying to extract the middle part of a few URLs like the ones below: s1 = "https://www.rocheplus.es/congresos-eventos/congresos-internacionales/oncologia/2020/astro.protected.html" s2 = "https://www.rocheplus.es/products/oncologia/erivedge/evidencias-cientificas.cugod.protected.html" s3 = "https://www.rocheplus.es/formacion/lung-link/estadios

If Else regex matching [duplicate]

感情迁移 提交于 2021-02-05 12:15:18
问题 This question already has answers here : regular expressions: match x times OR y times (4 answers) Closed 1 year ago . I want to build a regex where it searches for a string containing 12 digits in a row. If there's no match, look for a string with only 10 digits in a row. For example: a123456789012a a1234567890a Would return: 123456789012 And if the input is: a1234a a1234567890a It would return: 1234567890 I managed to create the regex for the individual operations, beeing (?<!\d)\d{10}(?!\d

Regex, match newlines between tags

僤鯓⒐⒋嵵緔 提交于 2021-02-05 12:13:01
问题 I have this regex in PHP: preg_match('/\[summary\](.+)\[\/summary\]/i', $data['text'], $match); It works fine when the text between the summary tags is on one line. However, when it contains newlines, it doesn't match. I've tried to find a correct modifier here: http://nl2.php.net/manual/en/reference.pcre.pattern.modifiers.php But the only one related to newlines is "m" and that doesn't do what I want. How to make this work? 回答1: The man page you've linked to describes another options that

Regular expression to get number between two square brackets

陌路散爱 提交于 2021-02-05 11:49:35
问题 Hi I need to get a string inside 2 pair of square brackets in javascript using regular expressions. here is my string [[12]],23,asd So far what I tried is using this pattern ' \[\[[\d]+\]\] ' and I need to get the value 12 using regular expressions 回答1: You can capture the digits using groups "[12]],23,asd".match(/\[\[(\d+)\]\]/)[1] => "12" 回答2: You can use the following regex, \[\[(\d+)\]\] This will extract 12 from [[12]],23,asd It uses capture groups concept 回答3: \[\[(\d+)\]\] Try this

Regular expression to get number between two square brackets

依然范特西╮ 提交于 2021-02-05 11:44:04
问题 Hi I need to get a string inside 2 pair of square brackets in javascript using regular expressions. here is my string [[12]],23,asd So far what I tried is using this pattern ' \[\[[\d]+\]\] ' and I need to get the value 12 using regular expressions 回答1: You can capture the digits using groups "[12]],23,asd".match(/\[\[(\d+)\]\]/)[1] => "12" 回答2: You can use the following regex, \[\[(\d+)\]\] This will extract 12 from [[12]],23,asd It uses capture groups concept 回答3: \[\[(\d+)\]\] Try this

Perl Named Captured Group

限于喜欢 提交于 2021-02-05 11:41:33
问题 I have created two named capture variables in the regex and the second one doesn't seem to return any value while the first one can. I am not sure why..here is the code. my $string = 'test [google] another test [windows]'; my $regex=qr/\w*\[{1}(?<firstBracket>\w+)\]{1}(?<secondBracket>\w*)/ip; $string=~ /$regex/; say $+{secondBracket}; I am expecting that "secondBracket" will return. I can do $+{firstBracket} , but not the second one...Can someone help please? Thanks. 回答1: You probably mean:

Perl Named Captured Group

一笑奈何 提交于 2021-02-05 11:41:06
问题 I have created two named capture variables in the regex and the second one doesn't seem to return any value while the first one can. I am not sure why..here is the code. my $string = 'test [google] another test [windows]'; my $regex=qr/\w*\[{1}(?<firstBracket>\w+)\]{1}(?<secondBracket>\w*)/ip; $string=~ /$regex/; say $+{secondBracket}; I am expecting that "secondBracket" will return. I can do $+{firstBracket} , but not the second one...Can someone help please? Thanks. 回答1: You probably mean:

find list of strings in list of strings, return boolean

隐身守侯 提交于 2021-02-05 11:34:26
问题 I am trying to work with lists of strings in python and somehow I can't find a good solution. I want to find a list of strings within a list of strings and return boolean values: import re sentences = ['Hello, how are you?', 'I am fine, how are you?', 'I am fine too, thanks'] bits = ['hello', 'thanks'] re.findall(sentences, bits) # desired output: [True, False, True] So I want to get an array of booleans with True, if the sentences string contains one or more of the bits. I also tried bits =