regex


How to get ultimate or penultimate string regex


I've got a list of strings and I would like to group using a regular expression every _ character if present
Test1_MYSTRING_val1
Test1_MYSTRING
I would like to obtain:
Test1, MYSTRING, val1
Test1, MYSTRING
This regex works for the first string but not for the second one: (Test[\d])_([\w]+?)_([\w]+)
Any suggest?
As already mentioned in the comments, using split is a better approach. However, if you still need to use Regex, you can do it this way:
(Test\d)_([A-Za-z0-9]+)(?:_([A-Za-z0-9]+))?
This will extract two or three capturing groups that are seperated by _
The first one is of the format: (Test\d)
The second one is of the format: ([A-Za-z0-9]+) and will have at least one character
The third one is of the same format ([A-Za-z0-9]+) and (?:_ ensures the underscore isn't captured and the whole thing is surrounded and marked with ? to indicate that it (the third group) may or may not be present
Note: I changed \w to [A-Za-z0-9] instead because remember \w matches underscores _ too
Regex 101 Demo

Related Links

Remove lines containing same string
Google Analytics - Exclude All Parameters Except One
Matching two strings in Python which has Escape character in it(E:()
How to define a regex that matches 4, 3 or 2 word with that hierarchic order?
Regex remove middle portion of the string with look ahead
Regular expression diffrence between [waz{2,3}up] and waz{2,3}up [duplicate]
Regular Expression String
Fuzzy string matching and regex
Apache mod rewrite is adding file extension automatically on mac
How to do Perl regex search-and-replace with indeterminate occurrances?
Nginx - redirect specific requests on root folder to subdomain
Match string “ces” in the Last Three Letters
Perl reg expression to match end of string doesnt work
vimscript regex: write multiple matches in array
shell script odd regex
Regex to match the hour portion of a time zone caption string

Categories

HOME
dynamic-programming
f#
compliance
apt-get
bolt-cms
backand
typeahead.js
chronicle
localforage
open-source
box
encog
wikipedia-api
jfreechart
avplayer
proftpd
sonata-admin
html5-video
fbx
lag
embed
syswow64
spring-boot-admin
eigen3
flow
round-robin
teraterm
drupal-commerce
abaqus
nsmutableattributedstring
appcelerator-alloy
zos
symbolic-math
epicorerp
yii1.x
x-sendfile
operator-keyword
clappr
swipe
argparse
folly
packaging
perl-module
partition
spring-saml
nexus-5
jquery-ui-dialog
zfs
distributed-lock
gulp-babel
log4perl
name-mangling
html-lists
jawr
jquery-ui-accordion
getelementbyid
postmark
tfs2008
git-config
codeigniter-upload
tealeaf
webclient-download
formatter
ios-frameworks
oracle-policy-automation
winobjc
main-activity
cucumber-java
occam-pi
jni4net
monkeyrunner
cbc-mode
error-log
xerces
fastparse
gitattributes
vraptor
cvs2git
computer-science-theory
sbrk
asf
private-methods
first-class-functions
master-theorem
textselection
xsltforms
disk-io
qt4.7
notifyicon
shift
rcu
jquery-data
couchpotato
sender
cocosbuilder
lungojs
datakey
media-manager
stumbleupon
ladon
combinators
replay
gedcom

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App