Reputation: 23
I need match a space but limit the matched with space with some condition..
Source : Code AS-277 Red/Yellow Code AS 278 Red: Code AS 279; Code AS-280 red; Code AS 279/ UPDATE : Needed Result: 1. match all word [\w] and few special char [ _-+*] for code 2. Stop char is [/;)] and COLOR CONSTAN [BLUE|YELLOW|ETC] 3. case insensitive AS-277 AS 278 AS 279 AS-280
I write on regex101.com https://regex101.com/r/S6mvZF/2
Upvotes: 2
Views: 188
Reputation: 626845
It seems you may use
'~Code\s+([\w+*]+(?:[-\s][\w+*]+)*?\b)\s?(?:red|blue|green|yellow|[);/])~i'
See the regex demo
Details
Code
- a substring\s+
- 1 or more whitespaces[\w+*]+
- 1 or more letters, digits, _
, *
or +
(?:[-\s][\w+*]+)*?
- zero or more (but as few as possible) sequences of:
[-\s]
- a -
or whitespace[\w+*]+
- 1 or more letters, digits, _
, *
or +
\b
- a word boundary (i.e. there must be a non-word char (non-letter, non-digit and non-_
) \s?
- 1 or 0 whitespaces(?:red|blue|green|yellow|[);/])
- one of the alternatives: red
, blue
, green
, yellow
(add more after |
), or [);/]
- a )
, ;
or /
.Grab Group 1 value upon a match.
Upvotes: 1
Reputation: 23892
You could do:
$string = preg_replace('/^.*Code (AS[- ]\d+).*$/m', '$1', $string);
Demo: https://regex101.com/r/S6mvZF/4
or
preg_match_all('/Code (AS[- ]\d+)/, $string, $matches);
and then $matches[1]
will have all your matches.
Both regexes look for Code AS
, then a hyphen or space, and then at least one number (if decimal places are limited a range can be applied {1,3}
). The first regex goes line by line. The second just captures each match.
Demo: https://regex101.com/r/S6mvZF/5
Upvotes: 1
Reputation: 5274
I think this should do the trick:
Code (AS[ -]\d{3})
It's
Here is the demo: https://regex101.com/r/S6mvZF/3
Good luck!
Upvotes: 0
Reputation: 321
Following your needed result, try this:
AS[- ][0-9]{3}
Upvotes: 0