ruby


Substring after a pattern


I have a string:
str = word1.woa.1398 word2.woa.1398 word3.woa.1398 word4.woa.1398 word5.woa.1398
I want to extract "1398" for word3. I need the value of path to be just "1398". When I use:
path = str[/(?:word3.woa.*)[0-9]+/]
I get:
"word3.woa.1398 word4.woa.1398 word5.woa.1398"
Can someone provide me the correct regex?
> s = "word1.woa.1398 word2.woa.1398 word3.woa.1398 word4.woa.1398 word5.woa.1398"
> s.match(/word3.woa.(\d+)/).captures[0]
=> "1398"
str = 'word2.woa.1398 word3.woa.1398 word4.woa.1398'
str[/(?<=\sword3\.woa\.)\d+/]
#=> "1398"
(?<=\sword3\.woa\.) is a positive lookbehind, requiring \d+ to be preceded immediately by \sword3\.woa\.
You may capture the part and get it easily using the current notation as:
path = str[/\bword3\.woa\.([0-9]+)/, 1]
path
# => 1398
Here, note that the dots are escaped to match literal dots, and the 1 means we need to extract only the contents of the first capturing group.
See the Ruby demo online. Also, see this Rubular demo (pay attention to "Match groups:" section).
Note that the pattern matches:
\b - a word boundary
word3\.woa\. - a word3.woa. literal substring
([0-9]+) - 1 or more digits captured into Group 1.
Ruby Onigmo regex library supports \K (match reset operator), so you may actually just match the left-hand context, and discard it by placing \K right after it, and grab the 1+ digits as a plain match text, no need for lookbehinds/capturing groups:
path = str[/\bword3\.woa\.\K[0-9]+/]
path
# => 1398
See another Ruby demo. Here, all you matched with \bword3\.woa\. is dropped and what is matched with [0-9]+ lands in the resulting match value.

Related Links

$stdin.gets is not working when execute ruby script via pipeline
ruby excel to csv adding .0 to zipcodes
How to add a Rake task to the default Rake task?
How to open secure connection in ruby
Can I change the XML node content using the Nokogiri .content method?
anything faster than .include? [duplicate]
Singleton methods in ruby
Why do we need to pop Hash to create this calculate method?
How do I create and test environment-specific code?
Why does this rails regex not work?
Ruby Tweetstream MongoDB Error
Need help creating a Compass `image-url` Sass function
How to make rspec see a method which is there, but probably it's class would need an instance?
why are warnings disabled by default in rake?
Open a new window with Ruby
How to replace UTF-8 characters with their character names in Ruby?

Categories

HOME
ribbon
salt
python-sphinx
windbg
modem
pawn
avfoundation
bi-publisher
equality
phone
android-edittext
tokbox
width
liferay-6
testng-eclipse
docker-volume
taskscheduler
smartgit
simulator
gdal
uitextfield
code-first
rpc
amazon-athena
icalendar
datetime-format
valgrind
neovim
organization
systemtap
unhandled-exception
mspec
rx.net
frame-rate
ruby-on-rails-3.2
big-theta
sidebar
activereports
state-space
bootbox
compiler-optimization
comet
gnulib
vdm++
setting
cinnamon
google-authenticator
qlistwidget
magento-1.3
chilkat
beanstream
github-desktop
d3plus
notepad2
computation
njsonschema
htk
pyglet
movieclip
pdf2htmlex
morphia
hardlink
opalrb
backbase-portal
spring-boot-maven-plugin
xlwt
ammap
python-gstreamer
twitter-bootstrap-tooltip
gift
adc
freshbooks-api
ng-storage
xcode7.2
nodetool
grunt-usemin
rdms
isomorphism
entry
vobject
c++builder-xe8
telerik-datepicker
sqlmx
libspotify
fontello
image-clipping
numerical-stability
beaker
invocationtargetexception
pagerjs
restrictions
django-signals
ci-merchant
jhtmlarea
hipaa
apache-vysper
android-lvl
java-interop
virtualfilesystem
managed-directx
ndbunit
future-proof
database-installation

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