I'm trying to category_id for purely numeric values, this works. I need to also capture category_name. For category_name, I need to capture until space or include space if it started with a double quote.
Sample user input string:
python c:192 c:1Stackoverflow c:"Stack Overflow2"
The desired captures should be these two values for category_name and the 192 for category_id.
Expected output:
1Stackoverflow
Stack Overflow2
The category_name must contain at least one non-digit, but can be all alpha with no digits.
This query partially works:
/c:(?<category_name>(?:")(?!\d+)[^"]+(?:")|(?!\d+)[^ ]+)/g
It doesn't capture the input 1Stackoverflow
, but does the quoted one. I need to remove the quotes:
(x.groups?.[key] ?? '').replace(/^\"/, '').replace(/\"$/, '')
The ?!\d+
is an attempt to evade clashing with category_id, but does not appear to be working.
How can I capture category_name in both forms (one word and quote deliminated) without the quotes in the capture and working with a leading digit?
To capture all 3 named groups in one regex use:
c:(?:(?<category_id>\d+\b)|("?)(?<category_name>(?:[^"\s]+|[^"]+))\2)
RegEx Breakdown:
c:
: Match c:
(?:
: Start non-capture group
(?<category_id>\d+\b)
: Named capture group category_id
to match 1+ digits followed by a word boundary|
: OR("?)
: Match "
or empty string and capture in group #2(?<category_name>[^"\s]+|[^"]+)
: Named capture group category_name
to match 1+ non-whitespace and non-double quote characters or 1+ of any char that is not a double quote\2
: Match same text as what we've capture in group #2 which is a "
or empty string)
: End non-capture group