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

Jekyll for podcast/xml - processing changes tag cases and content
How to make a string 1st letter caps, 2nd letter noncaps, 3rd letter caps, 4th letter noncaps…?
Amazon SNS with ruby, simple sms message
Generator of graphs with known chromatic number
.downto() method seems cannot be called on strings
Errno::ENOENT at / no such file or direcetory
ruby - How to replace a hash key/value with another key/value
How to switch tests depending on its context not in examples but in describe blocks
Difficulty understanding optional parentheses
How to accept input with Open3.popen2e in Ruby
Jekyll serve error - jekyll 3.2.1 Error: File exists # syserr_fail2_in
Gitlab warning in email notification on push after upgrade
Ruby will not return hash from function [closed]
NameError - uninitialized constant Sass::Engine:
how to get abstract from pubmed using PUBMEDid
Why my “gets” is not taking user input but rather printing the first line in the comment?

Categories

HOME
recursion
app-config
charts
ibm-content-navigator
kernel
bi-publisher
install4j
relational-database
dotvvm
forth
mondrian
typescript2.0
html5-history
azure-web-app-service
python-asyncio
nested
navicat
request
hl7
profiler
data-cleaning
converse.js
spring-cloud-task
documentation-generation
codeship
rule-engine
alfresco-webscripts
azure-api-management
netbeans-platform
interface-builder
sling
grub
rubocop
bond
chrome-web-store
java-web-start
android-speech-api
imagemagic
joi
last.fm
thingworx
ultragrid
windows64
ngtable
sharepoint-list
html-parsing
fsockopen
welcome-file
cinnamon
chained
google-vision
innobackupex
gwt-2.7
cpputest
feature-detection
css-sprites
mina
malware-detection
jsxgraph
ng-flow
xamarin.ios-binding
querying
software-quality
goo.gl
azure-vpn
maven-profiles
mtom
fann
chrome-gcm
xpages-extlib
mysql-connect
thinktecture
qtdbus
jinput
redbrick
array-multisort
viewwillappear
temboo
python-sip
kohana-3
flashlight
horn
nested-sortable
tfs-process-template
row-value-expression
cruisecontrol
digest
preg-split
nokia-n8
xcode3to4
securitymanager
desktop-sharing
instant-message

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