user3338391 user3338391 - 6 months ago 14
Java Question

check for a substring in a string before the first comma

I have a set of strings separated by

,

example:

abc,defg,ijkl

pqrs,tu,vv

,klmnop,qwe

aamn,nn,khhk


as you can see, string number 3 doesn't start with a substring. It starts with a comma.
using regex how can I tell a string starts with a substring of a random length before the first comma.

Answer

Description

^"[^"]+",

Regular expression visualization

This regular expression will do the following:

  • Verifies the string starts with a substring
  • Requires the substring to be any random length greater than zero

Example

Live Demo

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

Sample text

abc,defg,ijkl
pqrs,tu,vv
,klmnop,qwe
aamn,nn,khhk

Sample Matches

abc,
pqrs,
aamn,

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  [^,]+                    any character except: ',' (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  ,                        ','
----------------------------------------------------------------------
Comments