Sounds like you need an expression like this:
^[0-9a-zA-Z]+(,[0-9a-zA-Z]+)*$
Posix allows for the more self-descriptive version:
^[[:alnum:]]+(,[[:alnum:]]+)*$
^[[:alnum:]]+([[:space:]]*,[[:space:]]*[[:alnum:]]+)*$ // allow whitespace
If you're willing to admit underscores, too, search for entire words (\w+):
^\w+(,\w+)*
// allow whitespaces around the comma
Answer from Kerrek SB on Stack OverflowSounds like you need an expression like this:
^[0-9a-zA-Z]+(,[0-9a-zA-Z]+)*$
Posix allows for the more self-descriptive version:
^[[:alnum:]]+(,[[:alnum:]]+)*$
^[[:alnum:]]+([[:space:]]*,[[:space:]]*[[:alnum:]]+)*$ // allow whitespace
If you're willing to admit underscores, too, search for entire words (\w+):
^\w+(,\w+)*
// allow whitespaces around the comma
Try this pattern: ^([a-zA-Z0-9]+,?\s*)+$
I tested it with your cases, as well as just a single number "123". I don't know if you will always have a comma or not.
The [a-zA-Z0-9]+ means match 1 or more of these symbols
The ,? means match 0 or 1 commas (basically, the comma is optional)
The \s* handles 1 or more spaces after the comma
and finally the outer + says match 1 or more of the pattern.
This will also match
123 123 abc (no commas) which might be a problem
This will also match 123, (ends with a comma) which might be a problem.
You can give a name to your big regex in PCRE like:
(?<big>[a-zA-Z0-9]+)
Everything after the ?<name> will be recorded with the name given.
Called Regular Expression Subroutines
So, repeats (?&name) become easy:
^(?<big>[a-zA-Z0-9]+)(,(?&big))*$
Test it Online
So, matching an IP, for example, becomes simpler:
^(?<ip>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.(?&ip)){3}$
Test it OnLine.
Use it with grep as:
grep -P '^(?<ip>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.(?&ip)){3}$'
If using perl or PCRE regexps, you can avoid repetition by using things like (?1) to refer to the regexp in the first capture group:
grep -Px '(?:(\w{3}),)*(?1)'
Which would match on any non-empty comma separated list of 3 character words. Change to:
grep -Px '(?:(?:(\w{3}),)*(?1))?'
To allow an empty list.
Matching segment of comma separated string
Regex to mach string comma separated? - PHP - SitePoint Forums | Web Development & Design Community
regex for comma separated string (2 strings separated by comma)
csv - Regex for Comma delimited list - Stack Overflow
Hello, I'm having troubles matching a city name when a postcode is within the string. I'm able to grab the city and the postcode correctly between the 2 commas here: https://regex101.com/r/SFkcDv/1
Data
Street, City, Country 1 Street, 123 City, United States 2 Sreet, City 12345, United States Block 1 Street 100, City 123456, South Korea 120 Road, City 068913,Country 999 Road, City HA1 1NU, United Kingdom United States Australia Munich, Germany United States Philippines Germany USA
RegEx
(?<=,\s).+?(?=,)
However, I haven't found a way to just grab the city name without spaces and postcodes. The complexity also seems to come as UK postal codes have letters and numbers combined, in the following string:
Combwell NG17 1GQ London L4 2UY Manchester TS6 0SD Hyde DH6 1NX Oxford OX5 3HX
Any ideas on how to resolve this?
You can simply replace the middle number of each line with nothing.
In the editor
That is, in your editor, search-and-replace the regex
,[0-9]+,
(which only matches numbers with commas on both sides, which for your input is just the middle number) with a single comma:
,
I assume Ultraedit supports regex search-and-replace. If not, try Notepad++, which I know does.
From the command line
Since you tagged your question shell-script, here's how to do it from the command-line.
sed
Use sed, a standard Linux command also available for Windows as part of Cygwin or GnuWin32:
C:\>sed -e 's/,[0-9]+,/,/g' filename.txt
Powershell
Jens pointed out that you can also do it in Windows Powershell; see this explanation.
Regex syntax varies from application to application. I am unfamiliar with Ultredit and will give a generaql anser
Your regex lacks capturing parentheses
([0-9]+),[0-9]+,([0-9]+)
I suggest you to do in the following way:
(\d+)(,\s*\d+)*
which would work for a list containing 1 or more elements.
This regex extracts an element from a comma separated list, regardless of contents:
(.+?)(?:,|$)
If you just replace the comma with something else, it should work for any delimiter.
My regex: ([a-zA-Z],? *)+
The desired input is something like "A, B, C, D". But of course I want to allow input where the letters aren't capitalized, or there's more than one space between a letter+comma, or no space, etc.
Here's the regex I have, but it's not quite right because input such as "A B C D" is still considered valid.
([a-zA-Z],? *)+ which as I understand it (I'm new to regex) means: contains any letter from a to z, case insensitive, followed by zero or one comma, followed by 0 or more space(s), and then 1 or more of this entire pattern.
Now the issue I think has to do with the "zero or one comma" because I want to allow the input to have multiple letters eg. "A, B, C" but the last character shouldn't be followed by a comma, because that's weird. It also needs to match with single letter input which doesn't have a comma either. So what I want is it to be like a letter followed by a comma only if this isn't the last letter or the only letter... but I don't know how to do that with regex.
EDIT: Here is a new regex I made. I think this works: ([a-zA-Z] *, *)*([a-zA-Z] *)