regex-group

Why do I get the first capture group only?

耗尽温柔 提交于 2020-07-21 06:00:14
问题 (https://stackoverflow.com/a/2304626/6607497 and https://stackoverflow.com/a/37004214/6607497 did not help me) Analyzing a problem with /proc/stat in Linux I started to write a small utility, but I can't get the capture groups the way I wanted. Here is the code: #!/usr/bin/perl use strict; use warnings; if (open(my $fh, '<', my $file = '/proc/stat')) { while (<$fh>) { if (my ($cpu, @vals) = /^cpu(\d*)(?:\s+(\d+))+$/) { print "$cpu $#vals\n"; } } close($fh); } else { die "$file: $!\n"; } For

What does \0 mean in sed?

南笙酒味 提交于 2020-07-08 20:45:08
问题 I have the following code: echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/\1/p" It prints 12 as expected, since \1 refers to the first capturing group. However, if \0 is used instead of \1 , the output is 12. Chapter Name , the entire input string is printed. It seems that as long as the regex found a match, \0 prints the entire input string. Is this correct? I'm running Debian 10.2. 回答1: The \0 is a placeholder for the whole match value. Note that capturing group indices

What does \0 mean in sed?

倾然丶 夕夏残阳落幕 提交于 2020-07-08 20:44:29
问题 I have the following code: echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/\1/p" It prints 12 as expected, since \1 refers to the first capturing group. However, if \0 is used instead of \1 , the output is 12. Chapter Name , the entire input string is printed. It seems that as long as the regex found a match, \0 prints the entire input string. Is this correct? I'm running Debian 10.2. 回答1: The \0 is a placeholder for the whole match value. Note that capturing group indices

How to copy specific text from the body of the email?

纵饮孤独 提交于 2020-06-29 07:27:26
问题 Option Explicit Sub GetFromInbox() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim olFldr As Outlook.MAPIFolder Dim olItms As Outlook.Items Dim olMail As Variant Dim i As Long Set olApp = New Outlook.Application Set olNs = olApp.GetNamespace("MAPI") Set olFldr = olNs.GetDefaultFolder(olFolderInbox).Folders("impMail") Set olItms = olFldr.Items olItms.Sort "Subject" For Each olMail In olItms If InStr(olMail.Subject, "SubjectoftheEmail") > 0 Then ThisWorkbook.Sheets("Fixings")

How to copy specific text from the body of the email?

人走茶凉 提交于 2020-06-29 07:26:59
问题 Option Explicit Sub GetFromInbox() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim olFldr As Outlook.MAPIFolder Dim olItms As Outlook.Items Dim olMail As Variant Dim i As Long Set olApp = New Outlook.Application Set olNs = olApp.GetNamespace("MAPI") Set olFldr = olNs.GetDefaultFolder(olFolderInbox).Folders("impMail") Set olItms = olFldr.Items olItms.Sort "Subject" For Each olMail In olItms If InStr(olMail.Subject, "SubjectoftheEmail") > 0 Then ThisWorkbook.Sheets("Fixings")

Regex expression on strstr function returns empty capture group

余生长醉 提交于 2020-06-29 04:12:09
问题 The problem: using a hardcoded string, this works. However, using my dynamic content from $post_content, the regex returns an empty array. What I expect it to return is my expected output, where it gets all results from capture group 1 in an array from this this regex function. I have the following function: $post_content = (string)strstr($post_content, 'MyNeedle', true); If I print_r() the result of this function: "Je naam (verplicht) [text* your-test] Je e-mailadres (verplicht) [email* your

By using replace and regex: I have captured c, but I want to set it at the end of osonant plus “ay” with replace in Javascript

我的梦境 提交于 2020-04-30 06:49:12
问题 Here is he link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin. By using replace and regex: I have captured c , but I want to set it at the end of onsonant plus ay using the replace function in JavaScript Here is my code: function translatePigLatin(str) { let regEx=/([bcd-fgh-klmn-pqrst-vwxyz])/i console.log(str.replace(regEx, '$1,')) } translatePigLatin("consonant"); 回答1: See if that's what you want function

Use previous backreference as name of named capture group

﹥>﹥吖頭↗ 提交于 2020-02-28 07:27:00
问题 Is there a way to use a backreference to a previous capture group as the name of a named capture group ? This may not be possible, if not, then that is a valid answer. The following: $data = 'description: some description'; preg_match("/([^:]+): (.*)/", $data, $matches); print_r($matches); Yields: ( [0] => description: some description [1] => description [2] => some description ) My attempt using a backreference to the first capture group as a named capture group (?<$1>.*) tells me it's

Regex match a word not followed by two other words

大憨熊 提交于 2020-02-16 11:31:42
问题 I have some links: utmcsr=rdstation|utmccn=curso-intro-coaching|utmcmd=inbound|utmctr=link3 utmcsr=rdstation|utmccn=agenda-psc|utmcmd=email utmcsr=rdstation|utmccn=pnl-porto-alegre I want to build a regex expression that matches the rdstation not followed by inbound OR email . I've tried rdstation(?!(email|inbound)) but it doesn't worked. 回答1: The problem is that your negative lookahead is anchored to the position directly after rdstation. It would only exclude strings like this:

Unicode re.sub() doesn't work with \g<0> (group 0)

人盡茶涼 提交于 2020-02-03 13:25:47
问题 Why doesn't the \g<0> work with unicode regex? When I tried to use \g<0> to insert a space before and after the group with normal string regex, it works: >>> punct = """,.:;!@#$%^&*(){}{}|\/?><"'""" >>> rx = re.compile('[%s]' % re.escape(punct)) >>> text = '''"anständig"''' >>> rx.sub(r" \g<0> ",text) ' " anst\xc3\xa4ndig " ' >>> print rx.sub(r" \g<0> ",text) " anständig " but with unicode regex, the space isn't added: >>> punct = u""",–−—’‘‚”“‟„!£"%$'&)(+*-€/.±°´·¸;:=<?>@§#¡•[˚]»_^`≤…\«¿¨{}|