java


Regex optional matching


Pseudo/dummy-code that will be matched against:
RECOVERY: 'XXXXXXXXX' is UP
PROBLEM: 'ABABABAB' on 'XXXXXXXXX' is WARNING
PROBLEM: 'XXXXXXXXX' is DOWN
RECOVERY: 'ABABABAB' on 'XXXXXXXXX' is OK
PROBLEM: 'ABABABAB' on 'XXXXXXXXX' is DOWN
Goal
Capture XXXXXXXXX(without the single-quotes) but do NOT capture ABABABAB
Best attempt so far:
(M: \'|Y: \')(.*)(?:\' )(?:is)
Is it even possible to achive the goal above, and if yes, then how?
You can use a lookahead only to check if the string matched is before is:
'([^']*)'\\s*(?=\\bis\\b)
See regex demo
Breakdown:
' - single apostrophe
([^']*) - capture group matching 0 or more characters other than '
'\\s* - a single apostrophe and 0 or more whitespace symbols
(?=\\bis\\b) - a lookahead making sure there is a whole word is after the current position (after the ' with optional whitespaces)
Java demo:
Pattern ptrn = Pattern.compile("'([^']*)'\\s*(?=\\bis\\b)");
Matcher matcher = ptrn.matcher("RECOVERY: 'XXXXXXXXX' is UP");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
UPDATE
I used a lookahead only because you used a non-capturing group in your original regex : (?:is). A non-capturing group that has no quantifier set or any alternation inside seems reduntant and can be omitted. However, people often get misled by the name non-capturing thinking they can exclude the substring matched by this group from the overall match. To check for presence or absence of some text without matching, a lookaround should be used. Thus, I used a lookahead.
Indeed, in the current scenario, there is no need in a lookahead since it makes sense in case you need to match subsequent substrings that start wiyh the same sequence of characters.
So, a better alternative would be
'([^']*)'\s*is\b
Java:
Pattern ptrn = Pattern.compile("'([^']*)'\\s*is\\b");
Following regex should work
\'([^']+)\'\s+is
all the matches will be stored in matcher groups array

Related Links

Jackson error converting object to List
Streams with TreeMap return incoherent results
Use Maven proxy settings in custom plugin
Receiving Multipart Response on client side (ClosableHttpResponse)
Not able to fetch header data in rest assured for rest web service of type GET
How to read properties file from different location in JAVA
Value from button to be set in the next available JTextField?
How to use java code in rules created in drools workbench
How to upload files/multiple files from Node in Selenium
Blue Pelican Java Textbook Lesson 17 Project AddEmUp
How can I send parameters to methods using Maven/testng? [duplicate]
Setting environment variables and path in MacOS 10.11.6
Replace a pattern in a long String. Used replace method in a loop, but every time it will replace the first occurrence
java initialize object array
Add an element to an array of object
Are all instance variables declared outside functions?

Categories

HOME
winforms
atom-editor
tinymce
raspberry-pi
octobercms
getelementsbytagname
react-redux
gps
callback
ckeditor
yum
node-notifier
medical
tomcat6
modx-revolution
modelica
quickbooks
collectd
ibm-odm
designer
highlight.js
invantive-sql
apache-metamodel
oracle-coherence
jprofiler
zapier
visjs
cx-freeze
zurb-foundation-6
devops
format-specifiers
h2db
sqlcipher
facet
social-media
fluentvalidation
strncpy
saas
atlassian-crucible
subdomains
microsoft-chart-controls
google-rich-snippets
galsim
http-status-code-503
noraui
ghost4j
wallpaper
ios5
hot-module-replacement
libraries
streamreader
squib
email-templates
sfdc
midl
outlook-api
.net-4.6.2
websphere-mq-fte
youcompleteme
jedis
errordocument
hendrix
directory-structure
flow-control
django-scheduler
setuptools
nxt
pcf
autorest
testng-dataprovider
hexdump
asp.net5
tableau-online
twirl
interrupted-exception
nsight
iiviewdeckcontroller
proj4js
streambase
clicktag
applescript-objc
aapt
rtmfp
intentservice
floating-point-precision
dexterity
tidy
dotnetnuke-5
ember-app-kit
flashvars
ocunit
armcc
netbeans-6.9
bigcouch
stage
winbugs14
propagation
locate
digest-authentication
memory-size
swing-app-framework
eqatec
noscript
ntvdm.exe

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