Returning Only Part of Match from Regular Expression

Returning only part of match from Regular Expression

I like to use named groups:

Match m = Regex.Match("User Name:first.sur", @"User Name:(?<name>\w+\.\w+)");
if(m.Success)
{
string name = m.Groups["name"].Value;
}

Putting the ?<something> at the beginning of a group in parentheses (e.g. (?<something>...)) allows you to get the value from the match using something as a key (e.g. from m.Groups["something"].Value)

If you didn't want to go to the trouble of naming your groups, you could say

Match m = Regex.Match("User Name:first.sur", @"User Name:(\w+\.\w+)");
if(m.Success)
{
string name = m.Groups[1].Value;
}

and just get the first thing that matches. (Note that the first parenthesized group is at index 1; the whole expression that matches is at index 0)

Regex only returning first part of match

I would do

import re
text = "the cat sat on another mat"

re.findall('the\s+\w+|another\s+\w+', text)

The result should be

>>> ['the cat', 'another mat']

Extract just the part of string that matches a regex pattern in R

I added a capture group and a back-reference "\\1":

sub("^([A-Z]{1}[a-z]{2}\\s\\d+).*", "\\1", vector)
[1] "May 20" "Dez 1" "Oct 12"

The replacement argument accepts back-references like '\\1', but not typical regex patterns as you used. The back-reference refers back to the pattern you created and the capture group you defined. In this case our capture group was the abbreviated month and day which we outlined with parantheticals (..). Any text captured within those brackets are returned when "\\1" is placed in the replacement argument.

This quick-start guide may help

Regex match pattern x but return an arbitrary string y instead

The non-destructive /r modifier returns the changed string, if the pattern matched

my $arb_str =  $string =~ s/$pattern/arbitrary-string/r;

Available since 5.14.0

Python regex returns a part of the match when used with re.findall

Use a non-capturing group:

result = re.findall( r'\$[0-9]+(?:\.[0-9][0-9])?', inputstring)
^^

The re.findall function returns the list of captured texts if there are any defined in the pattern, and you have one in yours. You need to get rid of it by turning it into a non-capturing one.

re.findall(pattern, string, flags=0)

If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

Update

You can shorten your regex a bit by using a limiting quantifier {2} that requires exactly 2 occurrences of the preceding subpattern:

r'\$[0-9]+(?:\.[0-9]{2})?'
^^^

Or even replace [0-9] with \d:

r'\$\d+(?:\.\d{2})?'

Extract part of a regex match

Use ( ) in regexp and group(1) in python to retrieve the captured string (re.search will return None if it doesn't find the result, so don't use group() directly):

title_search = re.search('<title>(.*)</title>', html, re.IGNORECASE)

if title_search:
title = title_search.group(1)

Return only the regex match, not the whole line

grep(1) has a -o flag that outputs only the matching part of the line. From the man page:

  -o, --only-matching
Show only the part of a matching line that matches PATTERN.

Your pattern isn't right to get the output you want, though. Try:

$ egrep -o 'key \w+' file 
key word1
key word2

How to return part of javascript string that matches regex?

Use string.match function to return all the matched characters as array elements.

string.match(/[^\/,\s\?#]+?\.[^\/,\s]+?(?=\/|\s|$|\?|#)/g)

If you want to remove the matched portion then use string.replace

var s = string.replace(/[^\/,\s\?#]+?\.[^\/,\s]+?(?=\/|\s|$|\?|#)/g, "")


Related Topics



Leave a reply



Submit