Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I find the string with a Regular Expression in the example below that start with 'dd300' and end with a comma (result to include the comma)?


Example string:
dd301/122/4454,dd300/122/300,NR45,

Expected Result:
dd300/122/300,

What I have tried:

Can get the following to match dd300, but need to know how to include forward slashes and stop at the comma.

(?<!\w)dd300
Posted
Updated 19-Aug-18 23:02pm
Comments
Patrice T 20-Aug-18 4:47am    
Why is it a problem to match '/' ?

Special characters like the slash must be escaped with a back slash.

To match until a specific character occurs use .* or .+ follwed by that character. Because those are greedy (the term should be handled by every regex tuturioal), append a ?.

So the final regex might be:
(dd300\/.+?,)
The parentheses are only necessary when you need to access the matched string.

Depending on your input you may also use a more specific regex. If for example the dd300 is always follwed by two slash separated numbers it can be
(dd300\/\d+\/\d+,)
where \d specifies any digit which is the same as using [0-9].
 
Share this answer
 
The most basic would be just this:
dd300.*?,
Which is "'dd300'; followed by any character, any number of repetitions, as few as possible; then a comma".

If you are going to work with regexes, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900