LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-05-2018, 01:24 PM   #1
wrybread
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Rep: Reputation: 1
Help with a regular expression?


I'm trying to make a regular expression that will get the number after "ways" below (in this case a 4):

<control type="joy" player="1" buttons="1" ways="4"/>

I was given this regular expression which, I'm told, works in the Windows Powershell, but am having trouble making it work in bash:

(?<=ways=")(.*)(?="\/\>)

I'm trying this:

TEST_TEXT='<control type="joy" player="1" buttons="1" ways="4"/>'
echo $TEST_TEXT | grep '(?<=ways=")(.*)(?="\/\>)'

I don't imagine this is an easy fix for anyone?

And that's a simplified example. I think it's the same, but the full input text would be:

Code:
<?xml version="1.0"?>
<!DOCTYPE mame [
<!ELEMENT mame (machine+)>
	<!ATTLIST mame build CDATA #IMPLIED>
	<!ATTLIST mame debug (yes|no) "no">
	<!ATTLIST mame mameconfig CDATA #REQUIRED>
	<!ELEMENT machine (description, year?, manufacturer?, biosset*, rom*, disk*, device_ref*, sample*, chip*, display*, sound?, input?, dipswitch*, configuration*, port*, adjuster*, driver?, device*, slot*, softwarelist*, ramoption*)>
		<!ATTLIST machine name CDATA #REQUIRED>
		<!ATTLIST machine sourcefile CDATA #IMPLIED>
		<!ATTLIST machine isbios (yes|no) "no">
		<!ATTLIST machine isdevice (yes|no) "no">
		<!ATTLIST machine ismechanical (yes|no) "no">
		<!ATTLIST machine runnable (yes|no) "yes">
		<!ATTLIST machine cloneof CDATA #IMPLIED>
		<!ATTLIST machine romof CDATA #IMPLIED>
		<!ATTLIST machine sampleof CDATA #IMPLIED>
		<!ELEMENT description (#PCDATA)>
		<!ELEMENT year (#PCDATA)>
		<!ELEMENT manufacturer (#PCDATA)>
		<!ELEMENT biosset EMPTY>
			<!ATTLIST biosset name CDATA #REQUIRED>
			<!ATTLIST biosset description CDATA #REQUIRED>
			<!ATTLIST biosset default (yes|no) "no">
		<!ELEMENT rom EMPTY>
			<!ATTLIST rom name CDATA #REQUIRED>
			<!ATTLIST rom bios CDATA #IMPLIED>
			<!ATTLIST rom size CDATA #REQUIRED>
			<!ATTLIST rom crc CDATA #IMPLIED>
			<!ATTLIST rom sha1 CDATA #IMPLIED>
			<!ATTLIST rom merge CDATA #IMPLIED>
			<!ATTLIST rom region CDATA #IMPLIED>
			<!ATTLIST rom offset CDATA #IMPLIED>
			<!ATTLIST rom status (baddump|nodump|good) "good">
			<!ATTLIST rom optional (yes|no) "no">
		<!ELEMENT disk EMPTY>
			<!ATTLIST disk name CDATA #REQUIRED>
			<!ATTLIST disk sha1 CDATA #IMPLIED>
			<!ATTLIST disk merge CDATA #IMPLIED>
			<!ATTLIST disk region CDATA #IMPLIED>
			<!ATTLIST disk index CDATA #IMPLIED>
			<!ATTLIST disk writable (yes|no) "no">
			<!ATTLIST disk status (baddump|nodump|good) "good">
			<!ATTLIST disk optional (yes|no) "no">
		<!ELEMENT device_ref EMPTY>
			<!ATTLIST device_ref name CDATA #REQUIRED>
		<!ELEMENT sample EMPTY>
			<!ATTLIST sample name CDATA #REQUIRED>
		<!ELEMENT chip EMPTY>
			<!ATTLIST chip name CDATA #REQUIRED>
			<!ATTLIST chip tag CDATA #IMPLIED>
			<!ATTLIST chip type (cpu|audio) #REQUIRED>
			<!ATTLIST chip clock CDATA #IMPLIED>
		<!ELEMENT display EMPTY>
			<!ATTLIST display tag CDATA #IMPLIED>
			<!ATTLIST display type (raster|vector|lcd|unknown) #REQUIRED>
			<!ATTLIST display rotate (0|90|180|270) #REQUIRED>
			<!ATTLIST display flipx (yes|no) "no">
			<!ATTLIST display width CDATA #IMPLIED>
			<!ATTLIST display height CDATA #IMPLIED>
			<!ATTLIST display refresh CDATA #REQUIRED>
			<!ATTLIST display pixclock CDATA #IMPLIED>
			<!ATTLIST display htotal CDATA #IMPLIED>
			<!ATTLIST display hbend CDATA #IMPLIED>
			<!ATTLIST display hbstart CDATA #IMPLIED>
			<!ATTLIST display vtotal CDATA #IMPLIED>
			<!ATTLIST display vbend CDATA #IMPLIED>
			<!ATTLIST display vbstart CDATA #IMPLIED>
		<!ELEMENT sound EMPTY>
			<!ATTLIST sound channels CDATA #REQUIRED>
		<!ELEMENT input (control*)>
			<!ATTLIST input service (yes|no) "no">
			<!ATTLIST input tilt (yes|no) "no">
			<!ATTLIST input players CDATA #REQUIRED>
			<!ATTLIST input coins CDATA #IMPLIED>
			<!ELEMENT control EMPTY>
				<!ATTLIST control type CDATA #REQUIRED>
				<!ATTLIST control player CDATA #IMPLIED>
				<!ATTLIST control buttons CDATA #IMPLIED>
				<!ATTLIST control reqbuttons CDATA #IMPLIED>
				<!ATTLIST control minimum CDATA #IMPLIED>
				<!ATTLIST control maximum CDATA #IMPLIED>
				<!ATTLIST control sensitivity CDATA #IMPLIED>
				<!ATTLIST control keydelta CDATA #IMPLIED>
				<!ATTLIST control reverse (yes|no) "no">
				<!ATTLIST control ways CDATA #IMPLIED>
				<!ATTLIST control ways2 CDATA #IMPLIED>
				<!ATTLIST control ways3 CDATA #IMPLIED>
		<!ELEMENT dipswitch (dipvalue*)>
			<!ATTLIST dipswitch name CDATA #REQUIRED>
			<!ATTLIST dipswitch tag CDATA #REQUIRED>
			<!ATTLIST dipswitch mask CDATA #REQUIRED>
			<!ELEMENT dipvalue EMPTY>
				<!ATTLIST dipvalue name CDATA #REQUIRED>
				<!ATTLIST dipvalue value CDATA #REQUIRED>
				<!ATTLIST dipvalue default (yes|no) "no">
		<!ELEMENT configuration (confsetting*)>
			<!ATTLIST configuration name CDATA #REQUIRED>
			<!ATTLIST configuration tag CDATA #REQUIRED>
			<!ATTLIST configuration mask CDATA #REQUIRED>
			<!ELEMENT confsetting EMPTY>
				<!ATTLIST confsetting name CDATA #REQUIRED>
				<!ATTLIST confsetting value CDATA #REQUIRED>
				<!ATTLIST confsetting default (yes|no) "no">
		<!ELEMENT port (analog*)>
			<!ATTLIST port tag CDATA #REQUIRED>
			<!ELEMENT analog EMPTY>
				<!ATTLIST analog mask CDATA #REQUIRED>
		<!ELEMENT adjuster EMPTY>
			<!ATTLIST adjuster name CDATA #REQUIRED>
			<!ATTLIST adjuster default CDATA #REQUIRED>
		<!ELEMENT driver EMPTY>
			<!ATTLIST driver status (good|imperfect|preliminary) #REQUIRED>
			<!ATTLIST driver emulation (good|imperfect|preliminary) #REQUIRED>
			<!ATTLIST driver color (good|imperfect|preliminary) #REQUIRED>
			<!ATTLIST driver sound (good|imperfect|preliminary) #REQUIRED>
			<!ATTLIST driver graphic (good|imperfect|preliminary) #REQUIRED>
			<!ATTLIST driver cocktail (good|imperfect|preliminary) #IMPLIED>
			<!ATTLIST driver protection (good|imperfect|preliminary) #IMPLIED>
			<!ATTLIST driver savestate (supported|unsupported) #REQUIRED>
		<!ELEMENT device (instance*, extension*)>
			<!ATTLIST device type CDATA #REQUIRED>
			<!ATTLIST device tag CDATA #IMPLIED>
			<!ATTLIST device fixed_image CDATA #IMPLIED>
			<!ATTLIST device mandatory CDATA #IMPLIED>
			<!ATTLIST device interface CDATA #IMPLIED>
			<!ELEMENT instance EMPTY>
				<!ATTLIST instance name CDATA #REQUIRED>
				<!ATTLIST instance briefname CDATA #REQUIRED>
			<!ELEMENT extension EMPTY>
				<!ATTLIST extension name CDATA #REQUIRED>
		<!ELEMENT slot (slotoption*)>
			<!ATTLIST slot name CDATA #REQUIRED>
			<!ELEMENT slotoption EMPTY>
				<!ATTLIST slotoption name CDATA #REQUIRED>
				<!ATTLIST slotoption devname CDATA #REQUIRED>
				<!ATTLIST slotoption default (yes|no) "no">
		<!ELEMENT softwarelist EMPTY>
			<!ATTLIST softwarelist name CDATA #REQUIRED>
			<!ATTLIST softwarelist status (original|compatible) #REQUIRED>
			<!ATTLIST softwarelist filter CDATA #IMPLIED>
		<!ELEMENT ramoption (#PCDATA)>
			<!ATTLIST ramoption default CDATA #IMPLIED>
]>

<mame build="0.175 (unknown)" debug="no" mameconfig="10">
	<machine name="dkong" sourcefile="dkong.cpp">
		<description>Donkey Kong (US set 1)</description>
		<year>1981</year>
		<manufacturer>Nintendo of America</manufacturer>
		<rom name="c_5et_g.bin" size="4096" crc="ba70b88b" sha1="d76ebecfea1af098d843ee7e578e480cd658ac1a" region="maincpu" offset="0"/>
		<rom name="c_5ct_g.bin" size="4096" crc="5ec461ec" sha1="acb11a8fbdbb3ab46068385fe465f681e3c824bd" region="maincpu" offset="1000"/>
		<rom name="c_5bt_g.bin" size="4096" crc="1c97d324" sha1="c7966261f3a1d3296927e0b6ee1c58039fc53c1f" region="maincpu" offset="2000"/>
		<rom name="c_5at_g.bin" size="4096" crc="b9005ac0" sha1="3fe3599f6fa7c496f782053ddf7bacb453d197c4" region="maincpu" offset="3000"/>
		<rom name="s_3i_b.bin" size="2048" crc="45a4ed06" sha1="144d24464c1f9f01894eb12f846952290e6e32ef" region="soundcpu" offset="0"/>
		<rom name="s_3j_b.bin" size="2048" crc="4743fe92" sha1="6c82b57637c0212a580591397e6a5a1718f19fd2" region="soundcpu" offset="1000"/>
		<rom name="v_5h_b.bin" size="2048" crc="12c8c95d" sha1="a57ff5a231c45252a63b354137c920a1379b70a3" region="gfx1" offset="0"/>
		<rom name="v_3pt.bin" size="2048" crc="15e9c5e9" sha1="976eb1e18c74018193a35aa86cff482ebfc5cc4e" region="gfx1" offset="800"/>
		<rom name="l_4m_b.bin" size="2048" crc="59f8054d" sha1="793dba9bf5a5fe76328acdfb90815c243d2a65f1" region="gfx2" offset="0"/>
		<rom name="l_4n_b.bin" size="2048" crc="672e4714" sha1="92e5d379f4838ac1fa44d448ce7d142dae42102f" region="gfx2" offset="800"/>
		<rom name="l_4r_b.bin" size="2048" crc="feaa59ee" sha1="ecf95db5a20098804fc8bd59232c66e2e0ed3db4" region="gfx2" offset="1000"/>
		<rom name="l_4s_b.bin" size="2048" crc="20f2ef7e" sha1="3bc482a38bf579033f50082748ee95205b0f673d" region="gfx2" offset="1800"/>
		<rom name="c-2k.bpr" size="256" crc="e273ede5" sha1="b50ec9e1837c00c20fb2a4369ec7dd0358321127" region="proms" offset="0"/>
		<rom name="c-2j.bpr" size="256" crc="d6412358" sha1="f9c872da2fe8e800574ae3bf483fb3ccacc92eb3" region="proms" offset="100"/>
		<rom name="v-5e.bpr" size="256" crc="b869b8f5" sha1="c2bdccbf2654b64ea55cd589fd21323a9178a660" region="proms" offset="200"/>
		<device_ref name="z80"/>
		<device_ref name="i8257"/>
		<device_ref name="screen"/>
		<device_ref name="gfxdecode"/>
		<device_ref name="palette"/>
		<device_ref name="latch8"/>
		<device_ref name="latch8"/>
		<device_ref name="latch8"/>
		<device_ref name="mb8884"/>
		<device_ref name="speaker"/>
		<device_ref name="discrete"/>
		<chip type="cpu" tag="maincpu" name="Z80" clock="3072000"/>
		<chip type="cpu" tag="dma8257" name="Intel 8257" clock="3072000"/>
		<chip type="cpu" tag="soundcpu" name="MB8884" clock="6000000"/>
		<chip type="audio" tag="mono" name="Speaker"/>
		<chip type="audio" tag="discrete" name="DISCRETE"/>
		<display tag="screen" type="raster" rotate="90" width="256" height="224" refresh="60.606061" pixclock="6144000" htotal="384" hbend="0" hbstart="256" vtotal="264" vbend="16" vbstart="240" />
		<sound channels="1"/>
		<input players="2" coins="1" service="yes">
			<control type="joy" player="1" buttons="1" ways="4"/>
			<control type="joy" player="2" buttons="1" ways="4"/>
		</input>
		<dipswitch name="Lives" tag="DSW0" mask="3">
			<dipvalue name="3" value="0" default="yes"/>
			<dipvalue name="4" value="1"/>
			<dipvalue name="5" value="2"/>
			<dipvalue name="6" value="3"/>
		</dipswitch>
		<dipswitch name="Bonus Life" tag="DSW0" mask="12">
			<dipvalue name="7000" value="0" default="yes"/>
			<dipvalue name="10000" value="4"/>
			<dipvalue name="15000" value="8"/>
			<dipvalue name="20000" value="12"/>
		</dipswitch>
		<dipswitch name="Coinage" tag="DSW0" mask="112">
			<dipvalue name="5 Coins/1 Credit" value="112"/>
			<dipvalue name="4 Coins/1 Credit" value="80"/>
			<dipvalue name="3 Coins/1 Credit" value="48"/>
			<dipvalue name="2 Coins/1 Credit" value="16"/>
			<dipvalue name="1 Coin/1 Credit" value="0" default="yes"/>
			<dipvalue name="1 Coin/2 Credits" value="32"/>
			<dipvalue name="1 Coin/3 Credits" value="64"/>
			<dipvalue name="1 Coin/4 Credits" value="96"/>
		</dipswitch>
		<dipswitch name="Cabinet" tag="DSW0" mask="128">
			<dipvalue name="Upright" value="128" default="yes"/>
			<dipvalue name="Cocktail" value="0"/>
		</dipswitch>
		<configuration name="Video Hardware" tag="VIDHW" mask="1">
			<confsetting name="TKG-02 (Radarscope Conversion)" value="0"/>
			<confsetting name="TKG-04 (Two board set)" value="1" default="yes"/>
		</configuration>
		<port tag=":VIDHW">
		</port>
		<port tag=":DSW0">
		</port>
		<port tag=":IN2">
		</port>
		<port tag=":IN0">
		</port>
		<port tag=":VR2">
		</port>
		<port tag=":IN1">
		</port>
		<adjuster name="VR2 - DAC Volume" default="90"/>
		<driver status="good" emulation="good" color="good" sound="good" graphic="good" savestate="supported"/>
	</machine>
	<machine name="z80" sourcefile="src/devices/cpu/z80/z80.cpp" isdevice="yes" runnable="no">
		<description>Z80</description>
	</machine>
	<machine name="i8257" sourcefile="src/devices/machine/i8257.cpp" isdevice="yes" runnable="no">
		<description>Intel 8257</description>
	</machine>
	<machine name="screen" sourcefile="src/emu/screen.cpp" isdevice="yes" runnable="no">
		<description>Video Screen</description>
	</machine>
	<machine name="gfxdecode" sourcefile="src/emu/drawgfx.cpp" isdevice="yes" runnable="no">
		<description>gfxdecode</description>
	</machine>
	<machine name="palette" sourcefile="src/emu/emupal.cpp" isdevice="yes" runnable="no">
		<description>palette</description>
	</machine>
	<machine name="latch8" sourcefile="src/devices/machine/latch8.cpp" isdevice="yes" runnable="no">
		<description>8 bit latch</description>
	</machine>
	<machine name="mb8884" sourcefile="src/devices/cpu/mcs48/mcs48.cpp" isdevice="yes" runnable="no">
		<description>MB8884</description>
	</machine>
	<machine name="speaker" sourcefile="src/emu/speaker.cpp" isdevice="yes" runnable="no">
		<description>Speaker</description>
		<sound channels="1"/>
	</machine>
	<machine name="discrete" sourcefile="src/devices/sound/discrete.cpp" isdevice="yes" runnable="no">
		<description>DISCRETE</description>
		<sound channels="0"/>
	</machine>
</mame>

Last edited by wrybread; 04-05-2018 at 01:36 PM.
 
Old 04-05-2018, 02:32 PM   #2
bgstack15
Member
 
Registered: Jul 2017
Distribution: korora
Posts: 90

Rep: Reputation: Disabled
If you're going to use look-arounds with GNU grep, use the -P flag to show it's a perl-regexp. Look-around (look-aheads and look-behinds) are the parts in parentheses that have the question mark as the first character.

My man page from EL7:
Code:
       -P, --perl-regexp
              Interpret  PATTERN  as  a  Perl  regular  expression.   This  is  highly
              experimental and grep -P may warn of unimplemented features.
 
Old 04-05-2018, 02:36 PM   #3
wrybread
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 1
Aha, thanks for that.

Now my problem is that it's highlighting the found data in red (the "4"), but still showing the rest of the line. Any idea how to make it just show the relevant data?
 
Old 04-05-2018, 02:39 PM   #4
wrybread
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 1
Aha, got it. The "only matching" flag (-o):

./mame -lx dkong | grep -oP -m 1 '(?<=ways=")(.*)(?="\/\>)'

And the "-m 1" means just show the first result.

For the sake of Google: The above regex works with the MAME video game emulator to find the orientation of the joystick (either 4-way or 8-way).
 
Old 04-05-2018, 03:31 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Great, other variants
Code:
(?<=ways=")\d+
(?<=ways=")[^"]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] jhalfs sed: -e expression #1, char 55:Invalid preceding regular expression percy_vere_uk Linux From Scratch 10 07-22-2017 07:15 AM
[SOLVED] Need help with regular expression, /[a-z][\.\?!]\s+[A-Z]/ blueskynet Linux - Newbie 9 04-29-2012 01:41 PM
[SOLVED] Help with regular expression xavitxus Programming 3 08-28-2011 04:44 PM
regular expression Ammad Linux - General 5 08-01-2008 07:41 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration