1

I want to match before and after a specific string. I have the string 10 990 Points But only want to match everything before and after  

Ive tried .+?(?= ), but that only takes everything before  .

I expect the output of 10 990 Points To be "10990 Points"

Console.WriteLine("Pris: " +
    Regex.Match(
        ProductListItem.Descendants("span")
        .Where(node => node.GetAttributeValue("class", "")
        .Equals("item-card-details-price-amount")).FirstOrDefault().InnerText
, @"([0-9]+)( )([A-z\s0-9]+)"));
SablyTv
  • 49
  • 9

3 Answers3

2

There are several ways to do so. You can simply use capturing groups for doing that, such as this expression:

(.*)(\")([\"\w\s0-9]+)(\")(.*)

enter image description here


Edit:

For your special input, you might use this expression:

([0-9]+)( )([A-z\s0-9]+)

You might use \ for escaping any metachars based on language specific requirements. If you wish, you can add or reduce boundaries.

enter image description here

Graph

This graph shows how it works:

enter image description here

Replacement

You can simply use a string replace and replace your input string with '$1$3'.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    [Regexr Test](https://regexr.com/4dd3t) For me it matches the whole string – SablyTv May 05 '19 at 00:53
  • 1
    How do i do this in c#? I added my code. Thanks for the super fast replies! – SablyTv May 05 '19 at 01:01
  • 1
    Checked it [post](https://stackoverflow.com/questions/4740984/c-sharp-regex-matches-example) you sent. And this is what i did. Does not work ```c# Console.WriteLine("Pris: " + Regex.Match( ProductListItem.Descendants("span") .Where(node => node.GetAttributeValue("class", "") .Equals("item-card-details-price-amount")).FirstOrDefault().InnerText , @"([0-9]+)( )([A-z\s0-9]+)", "&1&3")); ``` – SablyTv May 05 '19 at 01:29
1

The reason the pattern .+?(?= ) only matches what is in front of   is because (?= is a positive lookahead assertion that does not consume any characters.

If you want to match all before   and the rest after you could use 2 capturing groups instead of 3 because you don't need the match for   itself.

In the replacement use the 2 groups $1$2

(.+?) (.+)

That will match

  • (.+?) Capturing group 1, match 1+ chars except an newline non greedy
  •   Match  
  • (.+) Capturing group 2, match 1+ chars except an newline

See a .NET regex demo

If you want to match the digits ([0-9]+) only, your code might look like:

Regex.Replace("10 990 Points", @"([0-9]+) (.*)", m => m.Groups[1].Value + m.Groups[2].Value)

Note

In this character class [A-z\s0-9]+ this part A-z matches more than the ranges a-z and A-Z

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

Fixed it. Using the

([0-9]+)( )([A-z\s0-9]+)

In c#, I used

Regex.Replace(string, @"([0-9]+)( )([A-z\s0-9]+)", m => m.Groups[1].Value + m.Groups[3].Value)

m.Groups[1].Value and m.Groups[3].Value are the groups in Regex.Match, these get replaced. You can see it working here.

SablyTv
  • 49
  • 9