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

Is it okay to inherit classes in java even if it is not meant for that job
BlackBerry - get error code 411
how to create draggable toggle button in Android
Permutation of a 2 dimensional arraylist
Cast a String object to XMLNode object
how to resolve eclipse error Source not found
Can Mirth send JavaBeans on JMS?
How are Java interfaces used? [closed]
Using a external jar. No suitable driver found for jdbc:sqlserver
Java synchronizing a method in a class
Hibernate : refresh on detached object - weird behavior
Java program Not running in Eclipse [duplicate]
Make Graphics from JPanel visible outside JPanel
stdint.h for Java? [duplicate]
Design an Avro schema basis on my JSON document
creation of jtable of this format in java

Categories

HOME
listview
dictionary
stock
office365api
react-router
framework7
malloc
twitter-bootstrap-4
disassembler
cross-browser
node-notifier
maude-system
row
visual-studio-cordova
decomposition
iolanguage
clearcase-ucm
vaadin7
django-simple-history
transformation
code-review
nodatime
mps
sqlcipher
vlsi
intel-pin
internet-explorer-9
pingfederate
jspm
srcset
bosh
galsim
android-nestedscrollview
ghost4j
wpf-controls
io-redirection
objectlistview
opshub
scrollable
filepicker
jquery-validate
blogengine.net
pebble-watch
nstextview
jspdf-autotable
lync-client-sdk
sonarlint-vs
account-kit
google-cdn
deadbolt-2
google-feed-api
vmware-tools
elements
transmitfile
php-parse-error
independentsoft
associative-array
historian
mutation-observers
rgeo
graph-drawing
iiviewdeckcontroller
graph-api-explorer
pretty-print
streambase
directoryservices
javafx-webengine
castle
valueconverter
hippomocks
braille
algebraic-data-types
openlaszlo
fmod
oam
spring-io
rabl
smtp-auth
wsdl-2.0
tinn-r
itmstransporter
anonymous-methods
typoscript2
chuck
bluepill
broken-links
twrequest
work-stealing
gcj
mod-auth
f#-powerpack
xtype
servicehost
winbugs14
celltable
oncheckedchanged
mongomapper
blitz++
dmx512
icanhaz.js
project-hosting
scripting-languages
genealogy

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