Bash: regex to find string using =~

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
barhaqodes
Level 3
Level 3
Posts: 112
Joined: Mon Mar 27, 2023 3:33 pm

Bash: regex to find string using =~

Post by barhaqodes »

I want to obtain this value as a result:

Code: Select all

	$sql_array = array(
		'SELECT'	=> 'f.*',
		'FROM'		=> array(
			FORUMS_TABLE		=> 'f'
		),
		'LEFT_JOIN'	=> array(),
	);

Code: Select all

space="	"
Bash

Code: Select all

        regex_needle=".*?^$space\); *?$"
        # I seek needle in prohledavany_block
        if [[ $prohledavany_block =~ $regex_needle ]]; then
            echo "Found: ${BASH_REMATCH[0]}" 
        else
            echo "Needle not found"
        fi

$prohledavany_block is this text (shorten)

Code: Select all

 	$sql_array = array(
		'SELECT'	=> 'f.*',
		'FROM'		=> array(
			FORUMS_TABLE		=> 'f'
		),
		'LEFT_JOIN'	=> array(),
	);

	if ($config['load_db_lastread'] && $user->data['is_registered'])
	{
		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.user_id = ' . $user->data['user_id'] . ' AND ft.forum_id = f.forum_id');
		$sql_array['SELECT'] .= ', ft.mark_time';
	}
	else if ($config['load_anon_lastread'] || $user->data['is_registered'])
	{
		$tracking_topics = $request->variable($config['cookie_name'] . '_track', '', true, \phpbb\request\request_interface::COOKIE);
		$tracking_topics = ($tracking_topics) ? tracking_unserialize($tracking_topics) : array();

		if (!$user->data['is_registered'])
		{
			$user->data['user_lastmark'] = (isset($tracking_topics['l'])) ? (int) (base_convert($tracking_topics['l'], 36, 10) + $config['board_startdate']) : 0;
		}
	}

	if ($show_active)
	{
		$sql_array['LEFT_JOIN'][] = array(
			'FROM'	=> array(FORUMS_ACCESS_TABLE => 'fa'),
			'ON'	=> "fa.forum_id = f.forum_id AND fa.session_id = '" . $db->sql_escape($user->session_id) . "'"
		);
(it is a bit longer)


The problem is that I am getting nothing with this:

Code: Select all

regex_needle=".*?^$space\); *?$"
And greedy results with this:

Code: Select all

regex_needle=".*?$space\); *?$"
which is faulty because i want to insert new line or to seek newline. This must to return the block

Can you help to fix the regex?

Why .*? is greedy?

Why ^$space does not find the feedline or newline?
barhaqodes
Level 3
Level 3
Posts: 112
Joined: Mon Mar 27, 2023 3:33 pm

Re: Bash: regex to find string using =~

Post by barhaqodes »

Topic has been solved on different forum:
https://community.unix.com/t/regex-to-r ... y/393610/7
with a success. So the main problem was that I tried PCRE regexes, which did not comply with POSIX used in bash. I got great and easy suggestion just to write the feedlines directly in the brackets like that (multiline regex!):

Code: Select all

 "
"
Actually it is not possible to enter the multiline string. It is stripped white spaces-

So super-easy.
Post Reply

Return to “Scripts & Bash”