PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to capture a regex group after the specific word?

 November 06, 2022     c#, regex, regex-group     No comments   

Issue

I am stuck with regex again. I have this example:

 3, 
SomethingThatShouldntMatch: 3, 5
Divisions: 1, 2, 3, 5, 13
Divisions: 33
Divisions: 3, 22
Divisions: 4, 55
Divisions: 10, 31

I want to find lines beginning with "Divisions:" and only in these lines capture the first specified number.

For example, if I put number "3" to search pattern it should return me only lines beginning with "Divisions:" and number 3 somewhere after the word.

Not 13, not 33, not 30. Only 3 or nothing. This pattern is planned to use in Regex.Replace().

The main problem I've ran into is that it won't continue searching if the specified number is not first after the word. "Divisions: 1, 2, 3" won't match but it should.

However, "Divisions: 3, 5" matches. I've tried about a dozen of expressions but I can't figure it out. What am I doing wrong? Why it doesn't search after the first comma?

[\n\r].*Divisions:?(\D3\D)?

https://regex101.com/r/ydqLGB/1


Solution

The pattern [\n\r].*Divisions:?(\D3\D)? does not match because it matches Divisions followed by an optional colon and then directly after is optionally matches a 3 char surrounded by non digits.

The only string where that matches in the examples is Divisions: 3, as the 3 is directly following and the \D matches the space before it and the comma after it.

Note that \D actually matches a character and the [\n\r] matches a mandatory newline.


If a single line like Divisions: 3 should also match, you might write the pattern as:

^Divisions:[ ,0-9]*\b(3)\b
  • ^ Start of string
  • Divisions: Match literally
  • [ ,0-9]* Match optional spaces, comma or digit 0-9
  • \b(3)\b Capture a 3 char between word boundaries

See a regex demo


If the colon is optional:

^Divisions:?[ ,0-9]*\b(3)\b


Answered By - The fourth bird
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing